【问题标题】:Array of objects condensed into array of unique objects with nested array将对象数组压缩为具有嵌套数组的唯一对象数组
【发布时间】: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


【解决方案1】:

我会使用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))

【讨论】:

  • 谢谢,但这只会嵌套传播和日期,不会通过唯一的 awayId 压缩
  • groupBy 是根据_id 还是awayId?
  • awayId,我在评论后才意识到。谢谢!
  • 你可以控制它:) GL