Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!