从这里:(这个数组是调用响应)
[
{ "DAY": 20190323,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190324,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190325,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190326,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190327,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
]到这里:
[
[20190323, "Instant Purification", "Pentatone A/B" , "This is a drill"],
[20190324, "Instant Purification", "Pentatone A/B" , "This is a drill"],
[20190325, "Instant Purification", "Pentatone A/B" , "This is a drill"],
[20190326, "Instant Purification", "Pentatone A/B" , "This is a drill"],
[20190327, "Instant Purification", "Pentatone A/B" , "This is a drill"]
]所以我就这么做了:
const yearDays = res.map(x => x['YEAR_DAY']);
const streams = res.map(x => x['STREAMNAME']);
const labeler = yearDays.map((v, i) => {return [v, String(streams[i]).split(/\s*(?:,|$)\s*/)]; });相反,我得到了:(这有点接近,但不是真的)
[20190323, ["Instant Purification", "Pentatone A/B" , "This is a drill"]
[20190324, ["Instant Purification", "Pentatone A/B" , "This is a drill"]
[20190325, ["Instant Purification", "Pentatone A/B" , "This is a drill"]
...如何从内部数组中取出所有元素,并使它们成为外部数组的一部分?
发布于 2019-03-31 10:02:09
您可以使用map()并返回具有DAY属性和拆分的STREAMNAME属性的新数组。您应该使用Spread Operator来使数组成为平面。
let arr = [
{ "DAY": 20190323,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190324,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190325,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190326,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
{ "DAY": 20190327,"NAME": "BTA130", "STREAMNAME": "Instant Purification, Pentatone A/B , This is a drill"},
]
let res = arr.map(({DAY,STREAMNAME})=>[DAY,...STREAMNAME.split(', ')])
console.log(res)
https://stackoverflow.com/questions/55437216
复制相似问题