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"},
]
到这里:
[
[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
属性的新数组。您应该使用扩展运算符使数组变平。
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)
推荐阅读
- c# - 仅当使用 linq QueryableExpression 在 c# 中对数据库进行异步调用时才获取 ArgumentException
- sql - 加入具有日期范围的表
- python - 使用正则表达式匹配一列中的模式并根据列属性名称创建一个新列
- unity3d - 如何修复:“GameObject”类型的对象已被破坏,但您仍在尝试访问它?-统一
- f# - 如何使用 Elmish 调试器解决编码器问题?
- javascript - 具有不同 ID 的第二个模态以不同的效果打开 - 为什么?
- c - web_convert_param 用于 loadrunner 中的序数值
- python - 在 pandas udf pyspark 中使用 numpy
- flask - 用于生产的 K8s 上的普通 Flask 服务器与基于 UWSGI 的服务器?
- c++ - 获取/检查进程 win32 的内部 kernel32 状态(为了安全使用 TerminateThread )