【发布时间】:2024-12-05 15:05:02
【问题描述】:
我有 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 的唯一对象,但我不确定使用过滤器是否允许我推送带有点差和日期的行。
【问题讨论】:
-
createdDates 来自哪里? -
只需在 SO 上搜索“按属性分组对象数组”
-
createdDate 在数据库中创建记录时设置
-
为什么输入
arr中缺少它? -
抱歉,更新了代码
标签: javascript arrays node.js mongodb chart.js