[英]How to convert nested array of object into arraylist in javascript?
i have a nested array of object and i want to convert in arraylist like this: this is my data array of object:我有一个嵌套的 object 数组,我想像这样在 arraylist 中转换:这是我的 object 数据数组:
{
"status": true,
"message": "",
"data": [{
"pasien_docs": [{
"ecg": null,
"date": "2020-01-21T05:22:01.901Z"
}, {
"ecg": 1.03,
"date": "2020-01-21T05:22:02.979Z"
}, {
"ecg": 1.04,
"date": "2020-01-21T05:22:04.053Z"
}, {
"ecg": 1.04,
"date": "2020-01-21T05:22:05.126Z"
},
]
}
]
}
and i want change convert to array like this:我想改变转换为这样的数组:
{
"status": true,
"message": "",
"data": [
[
"2020-01-21T05:22:01.901Z",
null
],
[
"2020-01-21T05:22:01.901Z",
1, 03
]
[
"2020-01-21T05:22:01.901Z",
1.04
]
[
"2020-01-21T05:22:01.901Z",
1.04
]
]
}
i try using map to convert on result like this:我尝试使用 map 转换结果如下:
result = result.map((u, i) => [
u.pasien_docs[i].date,
u.pasien_docs[i].ecg,
]);
but why i only get result data of one array not four data?但为什么我只得到一个数组的结果数据而不是四个数据? help me please, thankyou..
请帮帮我,谢谢。。
{
"status": true,
"message": "",
"data": [
[
"2020-01-21T05:22:01.901Z",
null
]
]
}
Would that work for you?这对你有用吗?
const src = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]}, result = {...src, data: src.data[0].pasien_docs.map(Object.values) } console.log(result)
.as-console-wrapper{min-height:100%;}
If you dont wanna use spread operator, this can also do the trick for you如果您不想使用扩展运算符,这也可以为您解决问题
const source = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]}
const result = Object.assign({}, source, {
data: source.data[0].pasien_docs.map(Object.values)
})
console.log(result)
let obj = { status: true, message: "", data: [ { pasien_docs: [ { ecg: null, date: "2020-01-21T05:22:01.901Z", }, { ecg: 1.03, date: "2020-01-21T05:22:02.979Z", }, { ecg: 1.04, date: "2020-01-21T05:22:04.053Z", }, { ecg: 1.04, date: "2020-01-21T05:22:05.126Z", }, ], }, ], }; var finalobj = JSON.parse(JSON.stringify(obj)); var innerobj = obj.data; var intermd = innerobj.map((data) => { return data.pasien_docs; }); finalarray = intermd[0].map((val) => { return [val.ecg, val.date]; }); console.log(obj); finalobj.data[0].pasien_docs=finalarray; console.log(finalobj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.