gpt4 book ai didi

javascript - 在数组内部分割一个字符串,然后将其从内部数组中取出,成为外部数组的一项

转载 作者:行者123 更新时间:2023-12-02 23:55:01 25 4
gpt4 key购买 nike

从这里:(此数组是调用响应)

 [
{ "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"]
...

如何从内部数组中获取所有元素并使它们成为外部数组的一部分?

最佳答案

您可以使用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)

关于javascript - 在数组内部分割一个字符串,然后将其从内部数组中取出,成为外部数组的一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55437216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com