User511
User511

Reputation: 1486

Remove duplicate data from array

I have 2 arrays.

const finalArr = [] 

const arr = ["abc","def"] 
const arr2 = [ 
               { name: "abc", refresh: false },
               { name: "efd", refresh: false },
               { name: "def", refresh: false } 
             ]

Now I am trying to update the value of refresh to true from arr2 for the name matches from arr, It returns duplicate values:

Here is my code:

arr2.map(obj => {
      arr.map(name => {

        if (obj.name === name){
          finalArr.push({ ...obj, refresh: true }) 
        } else{
          finalArr.push({ ...obj, refresh: false }) 
        }
      })

Can anybody tell me what is wrong with it?

Upvotes: 0

Views: 43

Answers (1)

Sulthan
Sulthan

Reputation: 130102

This is simple:

const finalArray = arr2.map(obj => {
   return { ...obj, refresh: arr.includes(obj.name) };
}

Upvotes: 3

Related Questions