Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda