【发布时间】:2020-01-07 14:35:39
【问题描述】:
我有这个:
[
[
{
users: {
'ID': {
stage: 21,
city: 'london',
data: 2345
},
'ID2': {
stage: 21,
city: 'london',
data: 5325
}
}
},
{
users: {
'ID': {
stage: 21,
city: 'ny',
data: 5761
},
'ID2': {
stage: 21,
city: 'ny',
data: 5235
}
}
},
{
users: {
'ID': {
stage: 21,
city: 'stockholm',
data: 7433
},
'ID2': {
stage: 21,
city: 'stockholm',
data: 52365
}
}
}
],
[
{
users: {
'ID': {
stage: 22,
city: 'london',
data: 743
},
'ID2': {
stage: 22,
city: 'london',
data: 5325
},
}
},
{
users: {
'ID': {
stage: 22,
city: 'ny',
data: 152
},
'ID2': {
stage: 22,
city: 'ny',
data: 61632
},
}
},
{
users: {
'ID': {
stage: 13,
city: 'stockholm',
data: 2161
},
'ID2': {
stage: 22,
city: 'stockholm',
data: 62176
},
}
}
]
]
我想要这样的东西:
[
{
id: 'ID',
stages: {
21: {
cities: {
london: {
data: 2345
},
ny: {
data: 5761
},
stockholm: {
7433
}
}
},
22: {
cities: {
london: {
data: 5325
},
ny: {
data: 5235
},
stockholm: {
52365
}
}
}
}
},
{
id: 'ID2',
// same idea for this user
}
]
data 只是作为示例,有更多元素,而不仅仅是一个
到目前为止,我尝试使用 reduce map 和/或 flat
array.flat().map(ar => Object.values(ar.players)).flat()
但我不确定将其展平是否是一个最佳主意,并且我不知道在它之后该怎么做,我尝试使用 reduce,但我的尝试都没有接近成功。
【问题讨论】:
标签: javascript arrays node.js object reduce