Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda