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

102
Visualizações
Trying to combine a .forEach and a .map function but I get return undefined

I have the following .map function that creates a new array of elements and changes their selected property to false:

const newAlteredData = alteredData.map((element) => {
  return { ...element, selected: false };
});

However, now I want to only change the elements that also exist in another array called changeRows

I tried the following:

const newAlteredData = alteredData.map((element) => {
  changeRows.forEach((changeRow) => {
    if (changeRow.deviceId === element.deviceId) {
      return { ...element, selected: false };
    }
    return element;
  });
});

However, this just return an array of undefined. Am I not using .map and/or .forEach correctly here?

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

0

Am I not using .map and/or .forEach correctly here?

forEach() is NOT the right method to use here. It is meant for iterating over an array - not finding elements inside it.

forEach() method doesn't not returns anything. Any value returned from its callback function is ignored.

Javascript provides multiple methods to find an element inside an array. You can use the find method to find an element in the changeRows array.

const newAlteredData = alteredData.map((element) => {
   const exists = changeRows.find(el => el.deviceId === element.deviceId);
 
   if (exists) {
      return { ...element, selected: false };
   }
});

Other methods that can be used:

  • findIndex
  • includes
  • some
about 4 years ago · Juan Pablo Isaza Relatório

0

As @sirius mentioned you forgot to return something from the forEach.

I would fix it this way:

const newAlteredData = alteredData.map((element) => {
  const arr = []
  changeRows.forEach((changeRow) => {
    if (changeRow.deviceId === element.deviceId) {
      arr.push({ ...element, selected: false });
    }
    arr.push(element);
  });
  return arr
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You can not return any thing inside a forEach.

Change this:

const newAlteredData = alteredData.map((element) => {
  changeRows.forEach((changeRow) => {
    if (changeRow.deviceId === element.deviceId) {
      return { ...element, selected: false };
    }
    return element;
  });
});

TO

const newAlteredData = alteredData.map((element) => {
  changeRows.find(changeRow => changeRow.deviceId === element.deviceId) {
     return { ...element, selected: false };
  }
  return element;
});
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