user3330820
user3330820

Reputation: 541

Merge two arrays of objects but have the second array pushed into an object key

I hope my terminology is correct on the title.

I have a fiddle of my code:

https://jsfiddle.net/v3yatpx8/

This is giving me undesirable results. You can see from the console.log(newArray) .

I want to push attMap into "info" for prodMap and have my final array structured like this:

[
   {
      "info" : [
      	{
           "attribute":"Grade:",
           "value":"Premium OR-Grade"
        },
        {
         "attribute":"Disposable or Reusable:",
         "value":"Reusable"
      	},
        {
         "attribute":"Handle:",
         "value":"Finger Rings"
      	},
      ],
      "name":"Product 1",
      "sku":"4242"
   }
]

I'm really not sure how to accomplish this. Any help would be appreciated!

Upvotes: 1

Views: 26

Answers (1)

Michael
Michael

Reputation: 591

Your object.assign statement is just a bit off.

newArray = prodMap.map((itm, i)=> ({
  ...itm,
  info: attMap[i]
}));

Map the array, keep each itm, and override its info property.

Upvotes: 1

Related Questions