Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
check same names in two array of objects in javascript /typescript?

I have two array of objects

array1=[{name:'apple',deleted:false},
       {name:'road',deleted:false},
       {name:'tree',deleted:false}]

and

array2=[{name:'apple'},
       {name:'tree'}]

what I want is : if array1 object has same names in array2, I want to change value of "deleted" in array1 to true . eg : {name:'apple',deleted:true}

expecting resulting array1:

array1=[{name:'apple',deleted:true},
           {name:'road',deleted:false},
           {name:'tree',deleted:true}]

here you can see , only apple, tree has deleted=true, because array2 has only 2 names : apple and tree.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can :

  • loop on array1 with forEach for sample

  • use some function to set deleted boolean

    deleted = array2.some(elem2 => elem2.name === elem.name)
    

const array1 = [{
    name: 'apple',
    deleted: false
  },
  {
    name: 'road',
    deleted: false
  },
  {
    name: 'tree',
    deleted: false
  }
]

const array2 = [{
    name: 'apple'
  },
  {
    name: 'tree'
  }
];

array1.forEach(elem => elem.deleted = array2.some(elem2 => elem2.name === elem.name));

console.log(array1);

about 4 years ago · Juan Pablo Isaza Report

0

Use forEach() to loop array1 and use include() to set value of deleted

const array1=[{name:'apple',deleted:false},
       {name:'road',deleted:false},
       {name:'tree',deleted:false}]
       
const array2=[{name:'apple'},
       {name:'tree'}]
       
array1.forEach(el=> el.deleted = array2.map(foo => foo.name).includes(el.name))
console.log(array1)

about 4 years ago · Juan Pablo Isaza Report

0

  1. Create a Set of deletedItems.

  2. Map over the items and update the deleted property using the Set.

const 
  items = [{ name: "apple", deleted: false }, { name: "road", deleted: false }, { name: "tree", deleted: false }],
  deletedItems = [{ name: "apple" }, { name: "tree" }],
  deletedSet = new Set(deletedItems.map((d) => d.name)),
  updatedItems = items.map((i) => (deletedSet.has(i.name) ? { ...i, deleted: true } : i));

console.log(updatedItems);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!