2021年3月22日星期一

comparing 2 array javascript

Hello Im trying to compare 2 array of object

and here is my code

const arr1 = [  {id:1,active:true},  {id:3,active:true},  {id:6,active:true},  {id:7,active:true},  ]      const arr2 = [  {id:1,active:false},  {id:2,active:false},  {id:3,active:false},  {id:4,active:false},  {id:5,active:false},  {id:6,active:false},  {id:7,active:false},    ]    let res = []     let ids = arr1.forEach((item) => {            return arr2.map((keyRow) => {              if (keyRow.id === item.id) {                keyRow.active = true              }              res.push(keyRow)             })          })      console.log(res)  

the proplem with this code is output double of array size my expected result is

[  {id:1,active:true},  {id:2,active:false},  {id:3,active:true},  {id:4,active:false},  {id:5,active:false},  {id:6,active:true},  {id:7,active:true}  ]  

how do I can achive that

https://stackoverflow.com/questions/66757140/comparing-2-array-javascript March 23, 2021 at 12:05PM

没有评论:

发表评论