Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
How to loop through two arrays and find common value object? Javascript using forEach

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!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda