0

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?

1 Answer 1

3

This is simple:

const finalArray = arr2.map(obj => {
   return { ...obj, refresh: arr.includes(obj.name) };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.