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

117
Vistas
How to filter an array of javascript objects based on an array of strings

Given the following object, I would like to search all of the keys for multiple strings. I've been trying and searching and have come up empty on this one. Can anyone provide me some help here?

Searching this:

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];

For this array of strings:

const query = ['chris', 'com'];

Would return an array of results like this:

[
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
]

Please let me know if I'm not being clear on this. Thanks!

// UPDATE

Note that the key thing I'm looking for is that each object must be able to be searched by ANY key (not just creator), and have ALL search strings in the object, not just one or the other.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

const array = [{
    name: "Blue Iron Chow Chow",
    status: "Complete",
    creator: "John"
  },
  {
    name: "Purple Steel Husky",
    status: "Error",
    creator: "Chris"
  },
  {
    name: "Purple Composite Husky",
    status: "Ready",
    creator: "Chris"
  },
  {
    name: "Aqua Zinc Spaniel",
    status: "Complete",
    creator: "Chris"
  },
  {
    name: "Fuschia Silver Corgi",
    status: "Complete",
    creator: "John"
  },
];

const query = ['chris', 'com'];

const filtered = array.filter(obj => {
  return query.every(val =>
  Object.values(obj).join(" ").toLowerCase().includes(val.toLowerCase()))
})

console.log(filtered)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];

let filtered = []


array.map((x)=>{
//x contains individual objects of array,
for (let key in x) {
//key in x will get all the keys inside the x , now all we have to check is does the values in x are included in query or not to find that we use the below code
  const contains = ['Chris', 'com'].includes(x[key])
   console.log(contains)
  if(contains) filtered.push(x)
}

})
about 4 years ago · Juan Pablo Isaza Denunciar

0

const array = [
    { name: "Blue Iron Chow Chow", status: "Complete", creator: "John" },
    { name: "Purple Steel Husky", status: "Error", creator: "Chris" },
    { name: "Purple Composite Husky", status: "Ready", creator: "Chris" },
    { name: "Aqua Zinc Spaniel", status: "Complete", creator: "Chris" },
    { name: "Fuschia Silver Corgi", status: "Complete", creator: "John" }, 
];


const query = ['chris', 'com'];

var result = array.filter(({creator, name, status}) => {
    return query.every(item => {
            item = item.toLowerCase();
            return creator.toLowerCase().includes(item) || name.toLowerCase().includes(item) || status.toLowerCase().includes(item)
        })
});

output:-

[    
  { name: 'Purple Composite Husky', status: 'Ready', creator: 'Chris' },
  { name: 'Aqua Zinc Spaniel', status: 'Complete', creator: 'Chris' }   
]
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