【问题标题】:Iterate array of object to merge new object迭代对象数组以合并新对象
【发布时间】:2019-08-07 03:18:57
【问题描述】:

我正在尝试使用数组创建新对象。我已经尝试过使用 lodash 地图。但我的问题是如何获取特定集合中的 product_ids 并将其放入数组中。

这是对象

[
  [
    {
      "id": "b7079be6-c9ab-4d24-9a1d-38eddde926c2",
      "collection": "mortgages",
      "product_id": "854174665132787596022"
    },
    {
      "id": "0da12779-6fe9-45d5-afbf-91d2466e5b7e",
      "collection": "mortgages",
      "product_id": "304285269353822734222"
    },
    {
      "id": "87a137ba-5f66-4e94-8d5d-698dfbaa08ec",
      "collection": "mortgages",
      "product_id": "-304414504724243127922"
    }
  ],
  [
    {
      "id": "522f4b83-4c5a-4ffe-836d-33a51325d251",
      "collection": "carinsurance",
      "product_id": "10413803"
    },
    {
      "id": "02b79566-9cf7-48fa-a8d3-eecf951b5a76",
      "collection": "carinsurance",
      "product_id": "10397803"
    }
  ]
]

我已经尝试过这段代码

map(this.products, (item, key) => {
   this.arr.push({
      collection: item[0].collection,
      product_ids: item.product_id
   });
});

我想要这样的结果。

{
    "collection": "mortgages",
    "product_ids": [
        "304285269353822734222",
        "854174665132787596022",
        "-304414504724243127922"
    ]
},
{
    "collection": "carinsurance",
    "product_ids": [
        "10413803",
        "10397803"
    ]
}

但我得到了这个结果。有人可以帮我解决这个问题吗?

{
    "collection": "mortgages",
    "product_ids": undefined
},
{
    "collection": "carinsurance",
    "product_ids": undefined
}

【问题讨论】:

    标签: javascript angular typescript lodash


    【解决方案1】:

    item 在您的情况下是一个数组,但是您正在访问它,就好像数组本身有一个 product_id 字段一样,您必须映射数组中每个项目的所有 product_id 属性:

    map(this.products, (item, key) => {
       return {
          collection: item[0].collection,
          product_ids: item.map(i => i.product_id)
       };
    });
    

    此外,如果 map 函数返回一个数组,对于初始数组中的每个项目,它会将其转换为新格式,因此您可以返回并分配给 this.arr,而不是在 map 函数中推送到 this.arr

    【讨论】:

    • 这只有在我们知道每个节点中的所有集合属性都相同时才有效。它适用于 OP 发布的数据集,但不适用于所有可能的数据集。
    • 这就是我要找的。你说得对,我忘了映射product_id。非常感谢。
    【解决方案2】:

    您可以使用嵌套的 reduce 函数。

    const collect = data => data.reduce((results, item) => {
      item.reduce((results, subItem) => {
        const current = results.find(i => i.collection === subItem.collection);
        if (current) {
          current.product_ids.push(subItem.product_id);
        } else {
          results.push({ collection: subItem.collection, product_ids: [subItem.product_id] });
        }
        return results;
      }, results);
      return results;
    }, []);
    
    const data = [
      [
        {
          "id": "b7079be6-c9ab-4d24-9a1d-38eddde926c2",
          "collection": "mortgages",
          "product_id": "854174665132787596022"
        },
        {
          "id": "0da12779-6fe9-45d5-afbf-91d2466e5b7e",
          "collection": "mortgages",
          "product_id": "304285269353822734222"
        },
        {
          "id": "87a137ba-5f66-4e94-8d5d-698dfbaa08ec",
          "collection": "mortgages",
          "product_id": "-304414504724243127922"
        }
      ],
      [
        {
          "id": "522f4b83-4c5a-4ffe-836d-33a51325d251",
          "collection": "carinsurance",
          "product_id": "10413803"
        },
        {
          "id": "02b79566-9cf7-48fa-a8d3-eecf951b5a76",
          "collection": "carinsurance",
          "product_id": "10397803"
        }
      ]
    ];
    
    console.log(collect(data));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 2018-08-15
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2014-07-11
      相关资源
      最近更新 更多