展平并组合对象数组
Flatten and combining an Array of Objects
我有一个像这样的对象数组
[
{
"id":1,
"name":"Competition One",
"entrants": [
{
"id":1,
"competitionId":1,
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"duration":"2 hours",
},
]
},
{
"id":2,
"name":"Competition Two",
"entrants": [
{
"id":3,
"competitionId":2,
"duration":"5 hours",
},
]
},
]
我想做的是得到一个只包含参赛者的平面数组,因此我正在这样做
const entrants = competitions.flatMap((comp) => {
return comp.entrants;
});
这似乎可以解决问题。我现在有以下
[
{
"id":1,
"competitionId":1,
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"duration":"2 hours",
},
{
"id":3,
"competitionId":2,
"duration":"5 hours",
}
]
但是,我想不通的是如何在上述数据中添加一个包含比赛名称的新字段。所以我追求的是这个
[
{
"id":1,
"competitionId":1,
"name": "Competition One"
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"name": "Competition One"
"duration":"2 hours",
},
{
"id":3,
"competitionId":2,
"name": "Competition Two"
"duration":"5 hours",
}
]
如何实现?我知道如何使用传统循环来做到这一点,但我试图强迫自己学习 ES6 的做事方式。这是 reduce 可以派上用场的地方吗?
感谢任何建议。
谢谢
映射时将name
属性合并到comp.entrants
中。
const entrants = competitions.flatMap((comp) => {
return {name: comp.name, ...comp.entrants};
});
您可以使用嵌套映射,然后将对象与额外的对象合并 属性:
let competitions = [{ "id":1, "name":"Competition One", "entrants": [{"id":1,"competitionId":1,"duration":"3 minutes",},{ "id":2,"competitionId":1,"duration":"2 hours",},]},{"id":2,"name":"Competition Two","entrants": [{"id":3,"competitionId":2,"duration":"5 hours",},]},]
let result = competitions.flatMap(({name, entrants}) =>
entrants.map(entrant => ({name, ...entrant}))
);
console.log(result);
使用 Array#map
获取具有相应 competition
名称的每个 entrants
列表:
const competitions = [
{
"id":1,
"name":"Competition One",
"entrants": [ { "id":1, "competitionId":1, "duration":"3 minutes" }, { "id":2, "competitionId":1, "duration":"2 hours" } ]
},
{
"id":2,
"name":"Competition Two",
"entrants": [ { "id":3, "competitionId":2, "duration":"5 hours" } ]
}
];
const entrants = competitions.flatMap(({ name, entrants = [] }) =>
entrants.map(entrant => ({ ...entrant, name }))
);
console.log(entrants);
我有一个像这样的对象数组
[
{
"id":1,
"name":"Competition One",
"entrants": [
{
"id":1,
"competitionId":1,
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"duration":"2 hours",
},
]
},
{
"id":2,
"name":"Competition Two",
"entrants": [
{
"id":3,
"competitionId":2,
"duration":"5 hours",
},
]
},
]
我想做的是得到一个只包含参赛者的平面数组,因此我正在这样做
const entrants = competitions.flatMap((comp) => {
return comp.entrants;
});
这似乎可以解决问题。我现在有以下
[
{
"id":1,
"competitionId":1,
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"duration":"2 hours",
},
{
"id":3,
"competitionId":2,
"duration":"5 hours",
}
]
但是,我想不通的是如何在上述数据中添加一个包含比赛名称的新字段。所以我追求的是这个
[
{
"id":1,
"competitionId":1,
"name": "Competition One"
"duration":"3 minutes",
},
{
"id":2,
"competitionId":1,
"name": "Competition One"
"duration":"2 hours",
},
{
"id":3,
"competitionId":2,
"name": "Competition Two"
"duration":"5 hours",
}
]
如何实现?我知道如何使用传统循环来做到这一点,但我试图强迫自己学习 ES6 的做事方式。这是 reduce 可以派上用场的地方吗?
感谢任何建议。
谢谢
映射时将name
属性合并到comp.entrants
中。
const entrants = competitions.flatMap((comp) => {
return {name: comp.name, ...comp.entrants};
});
您可以使用嵌套映射,然后将对象与额外的对象合并 属性:
let competitions = [{ "id":1, "name":"Competition One", "entrants": [{"id":1,"competitionId":1,"duration":"3 minutes",},{ "id":2,"competitionId":1,"duration":"2 hours",},]},{"id":2,"name":"Competition Two","entrants": [{"id":3,"competitionId":2,"duration":"5 hours",},]},]
let result = competitions.flatMap(({name, entrants}) =>
entrants.map(entrant => ({name, ...entrant}))
);
console.log(result);
使用 Array#map
获取具有相应 competition
名称的每个 entrants
列表:
const competitions = [
{
"id":1,
"name":"Competition One",
"entrants": [ { "id":1, "competitionId":1, "duration":"3 minutes" }, { "id":2, "competitionId":1, "duration":"2 hours" } ]
},
{
"id":2,
"name":"Competition Two",
"entrants": [ { "id":3, "competitionId":2, "duration":"5 hours" } ]
}
];
const entrants = competitions.flatMap(({ name, entrants = [] }) =>
entrants.map(entrant => ({ ...entrant, name }))
);
console.log(entrants);