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

168
Views
How to filter and map an array?

I have this object that I need to filter it.

let data = JSON.parse(this.getDataFromArray).map((x: any) => x.isEnabled === true);

I need to get in "data" only ID's of values that "isEnabled" is true.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Array.prototype.flatMap to achieve the result.

const dataArray = [
  { id: 1, name: "Alice", isEnabled: false },
  { id: 2, name: "Bob", isEnabled: true },
  { id: 3, name: "Charlie", isEnabled: true },
  { id: 4, name: "Dave", isEnabled: true },
  { id: 5, name: "Eve", isEnabled: false },
  { id: 6, name: "Frank", isEnabled: false },
];
let data = dataArray.flatMap(elm => elm.isEnabled ? [elm.id]: []);
console.log(data);

Please note that flatMap cannot be used in IE without a polyfill.

If you need IE support, use Array.prototype.forEach

const dataArray = [
  { id: 1, name: "Alice", isEnabled: false },
  { id: 2, name: "Bob", isEnabled: true },
  { id: 3, name: "Charlie", isEnabled: true },
  { id: 4, name: "Dave", isEnabled: true },
  { id: 5, name: "Eve", isEnabled: false },
  { id: 6, name: "Frank", isEnabled: false },
];
let data = [];
dataArray.forEach((elm) => {
  if (elm.isEnabled) {
    data.push(elm.id);
  }
});
console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

Try this to filter first, and map to desired property:

JSON.parse(this.getDataFromArray).filter((x: any) => x.isEnabled === true).map((result: any) => result.id);
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!