1

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!

3

2 Answers 2

2

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

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

2 Comments

I need forEach friend..i need forEach... I'm using angular 9 and new Subject ... I can't do that. I intentionally wrote forEach
@yajoj98337 code udpated with forEach also. This is even more simple than with Map
0
  • 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);

2 Comments

I need forEach friend..i need forEach... I'm using angular 9 and new Subject ... I can't do that. I intentionally wrote forEach
@yajoj98337 you can only use forEach? can you use find with it for example?

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.