将 objects 的数组与嵌套合并

Merging array of objects with nesting

我在根据 ID 组合 Objects 数组时遇到了一些小问题。我有三个数据集(删除数据以减少长度)

一套

[
   {
      "id":1,
      "name":"Some Name",
      "description":"Some description",
      "currency":"USD",
      "price":"350",
      "created_at":"2021-12-08T17:53:12.000Z",
   },
   {
      "id":2,
      "name":"Some Name",
      "description":"Some description",
      "currency":"USD",
      "price":"12",
      "created_at":"2021-12-08T17:53:12.000Z",
   },
]

设置二

[
   {
      "id":1,
      "resultOneId":1,
      "startDate":"2022-02-01T16:00:00.000Z",
      "endDate":"2022-02-08T18:00:00.000Z",
      "created_at":"2021-12-08T17:53:12.000Z",
   },
   {
      "id":2,
      "resultOneId":1,
      "startDate":"2021-02-08T09:00:00.000Z",
      "endDate":"2021-02-17T18:00:00.000Z",
      "created_at":"2021-12-08T17:53:12.000Z",
   },
    {
      "id":3,
      "resultOneId":2,
      "startDate":"2021-02-08T09:00:00.000Z",
      "endDate":"2021-02-17T18:00:00.000Z",
      "created_at":"2021-12-08T17:53:12.000Z",
   },
]

设置三

[
    {
        "id": 1,
        "resultTwoId": 1,
        "Job": "Technician",
    },
    {
        "id": 2,
        "resultTwoId": 1,
        "Job": "Electrician",
    },
    {
        "id": 3,
        "resultTwoId": 2,
        "Job": "Painter",
    },
    {
        "id": 4,
        "resultTwoId": 3,
        "Job": "Painter",
    },
    ...
]

从本质上讲,第二组是第一组的 child,第三组是第二组的智利。我的目标输出是这样的

[
   {
      "id":1,
      "name":"Some Name",
      "description":"Some description",
      "currency":"USD",
      "price":"350",
      "created_at":"2021-12-08T17:53:12.000Z",
      "resultTwos": [
          {
              "id":1,
              "resultOneId":1,
              "startDate":"2022-02-01T16:00:00.000Z",
              "endDate":"2022-02-08T18:00:00.000Z",
              "created_at":"2021-12-08T17:53:12.000Z",
              "resultThress": [
                {
                    "id": 1,
                    "resultTwoId": 1,
                    "Job": "Technician",
                },
                {
                    "id": 2,
                    "resultTwoId": 1,
                    "Job": "Electrician",
                },
              ]
           },
           {
              "id":2,
              "resultOneId":1,
              "startDate":"2021-02-08T09:00:00.000Z",
              "endDate":"2021-02-17T18:00:00.000Z",
              "created_at":"2021-12-08T17:53:12.000Z",
               "resultThress": [
                {
                    "id": 3,
                    "resultTwoId": 2,
                    "Job": "Painter",
                },
              ]
           },
      ]
   },
   {
      "id":2,
      "name":"Some Name",
      "description":"Some description",
      "currency":"USD",
      "price":"12",
      "created_at":"2021-12-08T17:53:12.000Z",
      "resultTwos": [
            {
              "id":3,
              "resultOneId":2,
              "startDate":"2021-02-08T09:00:00.000Z",
              "endDate":"2021-02-17T18:00:00.000Z",
              "duration":"5 hours",
              "created_at":"2021-12-08T17:53:12.000Z",
               "resultThress": [
                {
                    "id": 3,
                    "resultTwoId": 2,
                    "Job": "Painter",
                },
           },
      ]
   },
]

所以我对第一部分进行了排序,这将 resultsTwos 放在第一个结果中。

resultOnes.map( one => {return { ...one, resultTwos: resultTwos.filter(two => two.resultOneId === one.id)}})

然而,我正在努力让 resultThress 成为 resultTwos 的一部分,就像我在上面的示例输出中一样。

这是如何实现的?一直在尝试制作嵌套地图,但似乎无法正常工作。

感谢任何建议

谢谢

const a = [
  {
    "id": 1,
    "name": "Some Name",
    "description": "Some description",
    "currency": "USD",
    "price": "350",
    "created_at": "2021-12-08T17:53:12.000Z"
  },
  {
    "id": 2,
    "name": "Some Name",
    "description": "Some description",
    "currency": "USD",
    "price": "12",
    "created_at": "2021-12-08T17:53:12.000Z"
  }
];

const b = [
  {
    "id": 1,
    "resultOneId": 1,
    "startDate": "2022-02-01T16:00:00.000Z",
    "endDate": "2022-02-08T18:00:00.000Z",
    "created_at": "2021-12-08T17:53:12.000Z"
  },
  {
    "id": 2,
    "resultOneId": 1,
    "startDate": "2021-02-08T09:00:00.000Z",
    "endDate": "2021-02-17T18:00:00.000Z",
    "created_at": "2021-12-08T17:53:12.000Z"
  },
  {
    "id": 3,
    "resultOneId": 2,
    "startDate": "2021-02-08T09:00:00.000Z",
    "endDate": "2021-02-17T18:00:00.000Z",
    "created_at": "2021-12-08T17:53:12.000Z"
  }
];

const c = [
  {
    "id": 1,
    "resultTwoId": 1,
    "Job": "Technician"
  },
  {
    "id": 2,
    "resultTwoId": 1,
    "Job": "Electrician"
  },
  {
    "id": 3,
    "resultTwoId": 2,
    "Job": "Painter"
  },
  {
    "id": 4,
    "resultTwoId": 3,
    "Job": "Painter"
  }
];

const result = a.map(aObj => ({
    ...aObj,
    resultTwos: b.filter(bObj => bObj.resultOneId === aObj.id).map(bObj => ({
        ...bObj,
        resultThrees: c.filter(cObj => cObj.resultTwoId === bObj.id)

    }))
  }));

console.log("result", result);

要使用您的方法,您只需在 resultTwos 上的 filter 之后添加一个 map 并重复相同的逻辑:

resultOnes.map( aObj => {
  return { 
    ...aObj,
    resultTwos: resultTwos.filter(two => {two.resultOneId === aObj.id).map( bObj => {
      return {
        ...bObj,
        resultThrees: c.filter(cObj => cObj.resultTwoId === bObj.id)
      }
    }
  }
})

您也可以单独进行 three-into-two 映射,如果您以后再回来,这可能会使其更具可读性:

const mergedTwos = resultTwos.map( two => {
  ...two,
  resultThrees: c.filter(cObj => cObj.resultTwoId === bObj.id)
});

resultOnes.map( one => {
  return { 
    ...one,
    resultTwos: mergedTwos
  }
})

如果要有更多层,可能值得深入研究 array.reduce()