I have following two arrays, I want to get second array, but matching objects should contain one more extra key "select" : true, and not matching objects should contain "select" : false.
var firstArray = [{id: -1, type: "group"},
{id: -2, type: "group"}];
var secondArray = [{id: -3, type: "group"},
{id: -2, type: "group"},
{id: -1, type: "group"}];
Expected outcome:
var expectedArray = [{id: -1, type: "group", select: true},
{id: -2, type: "group", select: true},
{id: -3, type: "group", select: false}];
I have tried below code:
for(var i=0;i<firstArray.length;i++){
for(var j=0;j<secondArray.length;j++){
console.log(firstArray[i].id,secondArray[j].id)
if(firstArray[i].id===secondArray[j].id){
if(secondArray[j].hasOwnProperty('select')){
secondArray[j].select=true;
// console.log('true select property',secondArray[j].select)
}
else{
secondArray[j].select=true;
// console.log('true adding new',secondArray[j].select)
}
}else{
secondArray[j]['select']=false;
// console.log('false not match',secondArray[j].select)
}
}
}