Search code examples
javascriptarraysjavascript-objects

What is wrong in the following comparing and object property implementation it returns just 2 value (JS)?


I am trying to check if the first array includes the object that has the same id as the first element in the second array. Then for that object in the first array add the following 2 property values. I tried the following but it returns only an array of that new values instead of returning the full first array of objects that includes that modified object with new properties.

 products = products
        .filter(item => item.id === productsByTags[0].id)
        .map(item => (item.tags = 'shoes'), (item.keywords = 'black'))

How can I get the full array of objects where those 2 new properties, values are added to that object which has the same id based on above condition?


Solution

  • You need to return a new objectby spreading the actual object and two new properties.

    .map(item => ({ ...item, tags: 'shoes', keywords: 'black' }))