1

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)
               }
             }
          }
1
  • @user11299053, Sure, It works, Please upvote to my question. Commented Apr 30, 2019 at 3:09

4 Answers 4

4

You could create a Set of all the ids which are present in firstArray. Then loop through the secondArray using forEach and add select value based on whether the set has the id

let firstArray=[{id:-1,type:"group"},{id:-2,type:"group"}],
    secondArray=[{id:-3,type:"group"},{id:-2,type:"group"},{id:-1,type:"group"}];

const ids = new Set(firstArray.map(a => a.id));
secondArray.forEach(a => a.select = ids.has(a.id))

console.log(secondArray)

Sign up to request clarification or add additional context in comments.

Comments

1

You can do that with just one line of code:

var firstArray = [
	{id: -1, type: "group"}, 
	{id: -2, type: "group"}
];

var secondArray = [
	{id: -3, type: "group"}, 
	{id: -2, type: "group"}, 
	{id: -1, type: "group"}
];

const result = secondArray.map(item => ({...item, 'select': firstArray.some(entry => entry.id == item.id)}));

console.log(result);
.as-console-wrapper {
  max-height: 100% !important;
  color: white;
  top: 0;
}

.as-console-row {
  background: black;
}

Comments

1

This should work :

var ans = secondArray.map(s=> {
var match = firstArray.find(f=> f.id === s.id);
if(match) s['select'] = true;
else s['select'] = false;
return s;
})

Comments

1

Instead of modifying the secondArray, it is better to not modify it directly. We can use immutable way by creating another array that has select property. For this case, we will use map.

var firstArray = [{id: -1, type: "group"},
                  {id: -2, type: "group"}];

var secondArray = [{id: -3, type: "group"},
                   {id: -2, type: "group"},
                   {id: -1, type: "group"}];

const expectedArray = secondArray.map(item => {
  const idExist = firstArray.find(itemInFirst => itemInFirst.id === item.id);
  return {
    ...item,
    select: idExist !== undefined
  }
});

console.log(expectedArray);

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.