Vincent Nguyen
Vincent Nguyen

Reputation: 1663

Find matching elements between two arrays and update them

I think there is an easy solution but I've been working so long my brain is fried.

I have two arrays say for example:

[{name: "bob", age: 5},
 {name: "bob", age: 6}
 {name: "jim", age: 7}]

and an array I want to match against:

[{name: "bob", age: 5},
 {name: "bob", age: 6}]

I want to iterate through the first array and update all entries with name: bob to age + 1. So the resulting first array will be:

[{name: "bob", age: 6},
 {name: "bob", age: 7}
 {name: "jim", age: 7}]

I've tried a double for loop iterating through array1 then through array2 but the problem I get with that is I'll match bob twice, and in turn increment the age twice.

Upvotes: 0

Views: 80

Answers (6)

luis
luis

Reputation: 11

This is es5 format works for me:

    var arr = [{name: "bob", age: 5},{name: "bob", age: 6}, {name: "jim", age: 7}];
    for(var i = 0; i< arr.length; i++){
        if(arr[i].name == "bob"){
            arr[i].age+=1;
        }
    }
    console.log(arr);

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 10148

You can use forEach to loop through array and .find to find in the other array. If yes, increment the age else nothing

var arr = [{name: "bob", age: 5},
 {name: "bob", age: 6},
 {name: "jim", age: 7}]
 
 var arr1 = [{name: "bob", age: 5},
 {name: "bob", age: 6}]
 
 arr.forEach(e=> arr1.find(a=> a.name === e.name) ? e.age++ :null)
 
 console.log(arr)

Upvotes: 1

Nishant Dixit
Nishant Dixit

Reputation: 5522

var array1 = [{"name": "bob", "age": 5},
 {"name": "bob", "age": 6},
 {"name": "jim", "age": 7}];
 
 var array2 = [{"name": "bob", "age": 5},{"name": "bob", "age": 6}]

 
var array3 = array1.map(val =>{val.age =  array2.filter(obj => obj.name == val.name).length>0? val.age +1 : val.age; return val;});

console.log(array3);

Upvotes: 0

Anurag Singh Bisht
Anurag Singh Bisht

Reputation: 2753

You can use Array.map to iterate through the array1 and return the modified array.

Inside Array.map, find if the current object exists in array2 using the Array.find method. Update the age if object is found, other wise return the original array.

var array1 = [{name: 'bob', age: 5},
 {name: 'bob', age: 6},
 {name: 'jim', age: 7}]


var array2 = [{name: 'bob', age: 5}, {name: 'bob', age: 6}]

var array3 = array1.map(arr => {
  if (array2.find(a => a.name === arr.name && a.age === arr.age)) {
    arr.age++;
  }
  return arr;
});

console.log(array3);

Upvotes: 2

likeabbas
likeabbas

Reputation: 125

for (var i = 0; i < arr1.length(); i++)
  for (var j = 0; i < arr2.length(); j++)
    if (JSON.stringify(arr1[i]) === JSON.stringify(arr2[j])) {
      arr1[i].age += 1 

might be some small syntax errors since I whipped this out quickly

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138537

 const persons = [{name: "bob", age: 5}, {name: "bob", age: 6}, {name: "jim", age: 7}];

 const personsThatGotOlderToday = [{name: "bob" } /*...*/];

 for(const person of persons) {
   if(personsThatGotOlderToday.some(p => p.name === person.name)) 
     person.age++;
 }

However the array of people that got older should actually either just contain the persons name ... or it should be directly referencing the person objects, making the nested loop unneccessary.

Upvotes: 2

Related Questions