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

79
Views
Efficient Way of Filtering Array and Mapping in ES6

I wanted to get array that have status of "Existing" only and don't add the status in the newArray. What is the most efficient way of doing this?

const products = [
    {
        "id": "111",
        "name": "grapes",
        "status": "Linked",
    },
    {
        "id": "222",
        "name": "banana",
        "status": "Existing",
    },
    {
        "id": "333",
        "name": "mango",
        "status": "Existing",
    },
    {
      "id": "444",
      "name": "salad",
      "status": "Linked",
      
    },
    {
        "id": "555",
        "name": "juice",
        "status": "Existing",
    }
]

  const newArray = 
    products?.map(({ name = '', id = '' }) => ({
      name,
      id,
    }))
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think the problem is that you are trying to do both in one array function. I would filter it by status === "Existing" using filter, then map it, removing the status property.

const products = [{
    "id": "111",
    "name": "grapes",
    "status": "Linked",
  },
  {
    "id": "222",
    "name": "banana",
    "status": "Existing",
  },
  {
    "id": "333",
    "name": "mango",
    "status": "Existing",
  },
  {
    "id": "444",
    "name": "salad",
    "status": "Linked",

  },
  {
    "id": "555",
    "name": "juice",
    "status": "Existing",
  }
]

const newArray = products.filter((elem) => elem.status === "Existing")
     .map(({ id, name, status }) => ({ id, name }))

console.log(newArray);

about 4 years ago · Juan Pablo Isaza Report

0

The most efficient in terms of lines of code is probably just a filter() followed by a map() operation:

const result = products.filter(({status}) => status === 'Existing')
                       .map(({id, name}) => ({id, name}));

Full snippet:

const products = [{
  "id": "111",
  "name": "grapes",
  "status": "Linked",
}, {
  "id": "222",
  "name": "banana",
  "status": "Existing",
}, {
  "id": "333",
  "name": "mango",
  "status": "Existing",
}, {
  "id": "444",
  "name": "salad",
  "status": "Linked",

}, {
  "id": "555",
  "name": "juice",
  "status": "Existing",
}];

const result = products.filter(({status}) => status === 'Existing')
                       .map(({id, name}) => ({id, name}));
                       
console.log(result);

Since that requires a double iteration, the most efficient in terms of performance will probably be an explicit for loop that pushes matching values into a result array.

If you want to do everything in a single iteration but still maintain a functional approach, you could do everything with one reduce() operation:

const products = [{
  "id": "111",
  "name": "grapes",
  "status": "Linked",
}, {
  "id": "222",
  "name": "banana",
  "status": "Existing",
}, {
  "id": "333",
  "name": "mango",
  "status": "Existing",
}, {
  "id": "444",
  "name": "salad",
  "status": "Linked",

}, {
  "id": "555",
  "name": "juice",
  "status": "Existing",
}];

const result = products.reduce((a, {id, name, status}) => {
  if (status === 'Existing') a.push({id, name});
  return a;
}, []);
                       
console.log(result);

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!