【发布时间】:2021-05-05 15:03:08
【问题描述】:
我有一个从 API 返回的 JSON 数据,我想将其转换为数组,但无法将“时间线”类别放入数组中,
示例 JSON:
[{
"id": 100,
"key1": 310797,
"key2": 807874,
"key3": 24283,
"key4": 180900,
"timeline": {
"createDate": "2021-04-08T22:53:23Z",
"lastChangeDate": "2021-04-27T12:03:57Z"
}
},
{
"id": 101,
"key1": 310797,
"key2": 807875,
"key3": 24284,
"key4": 180903,
"timeline": {
"createDate": "2021-04-04T22:53:23Z",
"lastChangeDate": "2021-04-24T12:02:57Z"
},
{
...
}
]
我正在使用以下 Javascript 尝试将其解析为数组
function convertToArray(json) {
var headers = Object.keys(json[0]);
var values = json.map(function(e) {
return headers.map(function(f) {
return e[f]
})
});
values.unshift(headers);
return values
}
这是返回:
[[id, key1, key2, key3, key4, timeline],
[100, 310797, 807874, 24283, 180900, ""],
[101, 310797, 807875, 24284, 180903, ""]]
但是我想要实现的数组是:
[[id, key1, key2, key3, key4, createDate, lastChangeDate],
[100, 310797, 807874, 24283, 180900, 2021-04-08T22:53:23Z, 2021-04-27T12:03:57Z],
[101, 310797, 807875, 24284, 180903, 2021-04-04T22:53:23Z, 2021-04-24T12:02:57Z]]
任何关于如何将“createDate”和“lastChangeDate”映射到数组中的帮助将不胜感激
【问题讨论】:
标签: javascript arrays json