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

132
Vistas
Best way to filter an array by multiple properties?

I'm trying to filter an array and remove the entries that contain certain properties. For example, my array looks like this:

const items = [{a: "apple", b: "banana", c: "coconut"}, {a: "apple"}, {b: "banana, c: "coconut"}];

And I want to filter out the items that have the properties b or c.

Right now I have:

items.filter(item => "b" in item || "c" in item);

but I feel like there is a better way to do this, and set it up so a lot more properties could easily be added in the future.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Put the property names in an array, and use .some() to test if any of those properties exists.

const items = [{
  a: "apple",
  b: "banana",
  c: "coconut"
}, {
  a: "apple"
}, {
  b: "banana",
  c: "coconut"
}];

let properties_to_filter = ["b", "c"]
let result = items.filter(item => properties_to_filter.some(prop => prop in item));

console.log(result);

about 4 years ago · Santiago Trujillo Denunciar

0

      const items = [{a: "apple", b: "banana", c: "coconut"}, {a: "apple"}, {b: "banana", c: "coconut"}]

      let filtredKey = ["a"]

      let result = []
      items.map(el => {
        if(el.hasOwnProperty("c"))
          result.push(el)
      })

      console.log(result)

about 4 years ago · Santiago Trujillo 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