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

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

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 Denunciar

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 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