首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拆分数组内的字符串,然后将其从内部数组中取出,成为外部数组的一项

拆分数组内的字符串,然后将其从内部数组中取出,成为外部数组的一项
EN

Stack Overflow用户
提问于 2019-03-31 09:53:14
回答 1查看 41关注 0票数 0

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

代码语言:javascript
复制
 [
    { "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"},
 ]

到这里:

代码语言:javascript
复制
[
     [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"]
]

所以我就这么做了:

代码语言:javascript
复制
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*/)]; });

相反,我得到了:(这有点接近,但不是真的)

代码语言:javascript
复制
[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"]
...

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-31 10:02:09

您可以使用map()并返回具有DAY属性和拆分的STREAMNAME属性的新数组。您应该使用Spread Operator来使数组成为平面。

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55437216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档