【发布时间】:2020-08-12 13:52:36
【问题描述】:
如何以上述格式重新排列数据。我必须重新格式化数组并制作新的:
[
{
"cost":"1.3947648891000006",
"start_date":"2020-06-10",
"account":"1"
},
{
"cost":"1.4069091170999999",
"start_date":"2020-06-11",
"account":"1"
},
{
"cost":"1.401164025099997",
"start_date":"2020-06-11",
"account":"2"
},
{
"cost":"2.50928182113",
"start_date":"2020-06-12",
"account":"2"
}
]
到:
[
{
"start_date":"2020-06-10",
"account_1_cost":"1.3947648891000006"
},
{
"start_date":"2020-06-11",
"account_1_cost":"1.4069091170999999",
"account_2_cost":"1.401164025099997"
},
{
"start_date":"2020-06-12",
"account_2_cost":"2.50928182113"
}
]
reduce函数和map函数我都试过了,没用。
【问题讨论】:
-
所以您想通过
start_date安排这些帐户?那么,为什么不使用以日期为键的字典而不是数组呢? IE。 -{'2020-06-10' : (...), '2020-06-11': (...)} -
将数组对象解构为新对象数组。
标签: javascript arrays json sorting