【问题标题】:Split a string inside array , then take it out of inner array to become an item of external array拆分数组内部的字符串,然后将其从内部数组中取出,成为外部数组的一项
【发布时间】:2019-08-21 13:30:09
【问题描述】:

FROM HERE:(这个数组是调用响应)

 [
    { "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*/)]; });

INSTEAD IVE GOT:(有点接近但不是真的)

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

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

【问题讨论】:

  • @MaheerAli 怎么样?
  • 可能我复制错了。坚持
  • 再说一次,怎么会这样?这是一个带逗号的字符串。
  • @JackBashford 对不起,我的错误,代码很好。
  • 我已更新数组并将逗号放在末尾。还在 json 验证器上对其进行了验证。现在应该好了

标签: javascript arrays angular ecmascript-6


【解决方案1】:

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

【讨论】:

  • 优秀。我知道我没有看到任何东西,谢谢
  • @brohymn 您缺少的是使用扩展运算符。
猜你喜欢
  • 2018-12-15
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多