raf
raf

Reputation: 195

Iterate array of object to merge new object

I'm trying to create new object with array. I've already try it using lodash map. But my problem is how do i get the product_ids in specific collection and put it in array.

This is the object

[
  [
    {
      "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"
    }
  ]
]

I've already tried this code

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

I want to have a result like this.

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

But I am getting this result. Can somebody help me to this one.

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

Upvotes: 1

Views: 55

Answers (2)

Adrian Brand
Adrian Brand

Reputation: 21658

You could use nested reduce functions.

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));

Upvotes: 0

Dan D
Dan D

Reputation: 2523

item in your case is an array, but you are accessing it as if there was a product_id field on the array itself, you'll have to map all product_id properties from each item in the array:

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

Also, if the map function returns an array, for each item in the initial array it transforms it into a new format, so you can return and assign to this.arr instead of pushing to this.arr within the map function.

Upvotes: 4

Related Questions