Home > OS >  How to loop through two arrays and find common value object? Javascript using forEach
How to loop through two arrays and find common value object? Javascript using forEach

Time:10-07

I need to looping thought two arrays and find where is value expanded = true set to first array?

example of first array:

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = false },
 { id: 3 , name: 'Test 3' , expended = false }
]

exaple of second array:

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = true },
 { id: 3 , name: 'Test 3' , expended = false }
]

Different between two array is only expended value where is on second array is :

 { id: 2 , name: 'Test 2' , expended = true }

I need to loop thought first array and second and find in second array where is expended = true and just paste on first array.

The expected result above after the loop should be:

first array value

[ 
 { id: 1 , name: 'Test 1' , expended = false },
 { id: 2 , name: 'Test 2' , expended = true },
 { id: 3 , name: 'Test 3' , expended = false }
]

Please, I can't just copy the value of all array!

this example is not accepted:

this.secondArray = this.firstArray..

This is a simple example I sent you. I have 10 more parameters in the object that are changing, but it is only important for me to be expended to copy!

Literally, the value expended from the second array to copy to the first and that's it!

CodePudding user response:

You can achieve the result using Map and map

You can set the value of expended if value of expended of arr1 is false as

arr1.map((o) => ({ ...o, expended: o.expended || map.get(o.id).expended }))

const arr1 = [
  { id: 1, name: "Test 1", expended: false },
  { id: 2, name: "Test 2", expended: false },
  { id: 3, name: "Test 3", expended: false },
];

const arr2 = [
  { id: 1, name: "Test 1", expended: false },
  { id: 2, name: "Test 2", expended: true },
  { id: 3, name: "Test 3", expended: false },
];

const map = new Map(arr2.map((o) => [o.id, o]));

const result = arr1.map((o) => ({ ...o, expended: o.expended || map.get(o.id).expended }));
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you want the result with forEach

const arr1 = [
  { id: 1, name: "Test 1", expended: false },
  { id: 2, name: "Test 2", expended: false },
  { id: 3, name: "Test 3", expended: false },
];

const arr2 = [
  { id: 1, name: "Test 1", expended: false },
  { id: 2, name: "Test 2", expended: true },
  { id: 3, name: "Test 3", expended: false },
];

arr1.forEach((obj) => {
  const elementInArr2 = arr2.find((o) => o.id === obj.id);
  if (elementInArr2) obj.expended = obj.expended || elementInArr2.expended;
});

console.log(arr1);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

  • Using Array#reduce, iterate over the second array while updating a Map, if the object's expended is true, set the key to the id and the value to the object
  • Using Array#map, iterate over the first array. For every object check if the above map has its id and replace it, otherwise keep it

let 
  arr1 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: false }, { id: 3, name: 'Test 3', expended: false } ],
  arr2 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: true }, { id: 3, name: 'Test 3', expended: false } ];
  
const expenededMap = arr2.reduce((map, obj) => {
  if(obj.expended) map.set(obj.id, obj);
  return map;
}, new Map);

arr1 = arr1.map(obj => {
  if(obj.expended) return obj;
  const objFromArr2 = expenededMap.get(obj.id) || {};
  return objFromArr2.expended ? objFromArr2 : obj;
});

console.log(arr1);

Using just Array#forEach and Array#find:

let 
  arr1 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: false }, { id: 3, name: 'Test 3', expended: false } ],
  arr2 = [ { id: 1, name: 'Test 1', expended: false }, { id: 2, name: 'Test 2', expended: true }, { id: 3, name: 'Test 3', expended: false } ];
  
arr1.forEach((obj, i, arr) => {
  if(!obj.expended) {
    const objFromArr2 = arr2.find(({ id }) => id === obj.id) || {};
    if(objFromArr2.expended) arr[i] = objFromArr2;
  }
});

console.log(arr1);

  • Related