简体   繁体   中英

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.

 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.

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); 

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) 

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

 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); 

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM