gpt4 book ai didi

javascript - 对象数组压缩为具有嵌套数组的唯一对象数组

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:51 24 4
gpt4 key购买 nike

我在 MongoDB 中有 arr 数据,需要将文档压缩为一个,以便我可以在 Chartjs 中显示数据。

arr = [
{
"_id": "5d7baef782e09dc7f6b5be2d",
"awaySpread": "3.0",
"homeSpread": "-3.0",
"homeTeam": "Tennessee Titans",
"awayId": "4529605_261_sp",
"awayTeam": "Indianapolis Colts",
"homeId": "4529605_262_sp",
"eventDate": "2019-09-15 13:00:00",
"createdDate": "2019-09-13T13:00:06.527Z"
},
{
"_id": "5d7baef782e09dc7f6b5be31",
"awaySpread": "-19.5",
"homeSpread": "19.5",
"homeTeam": "Miami Dolphins",
"awayId": "4529609_269_sp",
"awayTeam": "New England Patriots",
"homeId": "4529609_270_sp",
"eventDate": "2019-09-15 13:00:00",
"createdDate": "2019-09-13T13:00:06.527Z"
},
{
"_id": "5d7baef782e09dc7f6b5be2d",
"awaySpread": "2.5",
"homeSpread": "-2.5",
"homeTeam": "Tennessee Titans",
"awayId": "4529605_261_sp",
"awayTeam": "Indianapolis Colts",
"homeId": "4529605_262_sp",
"eventDate": "2019-09-15 13:00:00",
"createdDate": "2019-09-13T13:30:06.527Z"
}
]

希望将其纳入:

newarr = [
{
"_id": "5d7baef782e09dc7f6b5be2d",
"awaySpread": "3.0",
"homeSpread": "-3.0",
"homeTeam": "Tennessee Titans",
"awayId": "4529605_261_sp",
"awayTeam": "Indianapolis Colts",
"homeId": "4529605_262_sp",
"eventDate": "2019-09-15 13:00:00",
"createdDate": "2019-09-13T13:00:06.527Z",
"lines": {
"spread": ["3.0","2.5"],
"dates: : ["2019-09-13T13:00:06.527Z","2019-09-13T13:30:06.527Z"]
},

},
{
"_id": "5d7baef782e09dc7f6b5be31",
"awaySpread": "-19.5",
"homeSpread": "19.5",
"homeTeam": "Miami Dolphins",
"awayId": "4529609_269_sp",
"awayTeam": "New England Patriots",
"homeId": "4529609_270_sp",
"eventDate": "2019-09-15 13:00:00"
"createdDate": "2019-09-13T13:00:06.527Z",
"lines": {
"spread": ["-19.5"],
"dates: : ["2019-09-13T13:00:06.527Z"]
},
}
]

我可以使用过滤器仅返回基于awayId的唯一对象,但我不确定使用过滤器是否允许我推送带有点差和日期的行。

最佳答案

我会使用reduce功能。

我正在构建一个对象,其中 id 是键,值是对象。当我已经有了里面的 id 时(第二次遇到相同的 id),我将相关值推送到行。

最后,我控制台记录了您想要的输出的对象的值。

arr = [
{
"_id": "5d7baef782e09dc7f6b5be2d",
"awaySpread": "3.0",
"homeSpread": "-3.0",
"homeTeam": "Tennessee Titans",
"awayId": "4529605_261_sp",
"awayTeam": "Indianapolis Colts",
"homeId": "4529605_262_sp",
"eventDate": "2019-09-15 13:00:00"
},
{
"_id": "5d7baef782e09dc7f6b5be31",
"awaySpread": "-19.5",
"homeSpread": "19.5",
"homeTeam": "Miami Dolphins",
"awayId": "4529609_269_sp",
"awayTeam": "New England Patriots",
"homeId": "4529609_270_sp",
"eventDate": "2019-09-15 13:00:00"
},
{
"_id": "5d7baef782e09dc7f6b5be2d",
"awaySpread": "2.5",
"homeSpread": "-2.5",
"homeTeam": "Tennessee Titans",
"awayId": "4529605_261_sp",
"awayTeam": "Indianapolis Colts",
"homeId": "4529605_262_sp",
"eventDate": "2019-09-15 13:00:00"
}
]

const newArr = arr.reduce((result, itr) => {
if (result[itr.awayId]) {
result[itr.awayId].lines.spread.push(itr.awaySpread)
result[itr.awayId].lines.dates.push(itr.eventDate)
} else {
result[itr.awayId] = { ...itr, lines: { spread: [itr.awaySpread], dates: [itr.eventDate] }}
}
return result
}, {})

console.log(Object.values(newArr))

关于javascript - 对象数组压缩为具有嵌套数组的唯一对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57936231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com