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

144
Visualizações
How to search an array of objects with a keyword that can match values to more than one property using filter
const heroes = [
  {name: "Batman", realName: "Bruce Wayne", universe: "DC"},
  {name: "Superman", realName: "Clark Kent", universe: "DC"},
  {name: "Iron Man", realName: "Tony Stark", universe: "Marvel"},
  {name: "Star Lord", realName: "Peter Quill", universe: "Marvel"},
  {name: "Venom", realName: "Eddie Brock", universe: "Marvel"}
]

function search(lookup){
  const result = heroes.filter(each => {
    return each.name.toLowerCase().includes(lookup.toLowerCase())  || 
           each.universe.toLowerCase().includes(lookup.toLowerCase())
  })
  return result
}
console.log(search("DC"))

If I search by name "Batman" I will return the first object. If I search "DC" I will return the first two objects. I'm getting the results I want but what other ways can I iterate over heroes instead of using the || operator? If I wanted to also search by realName I would have to add another return value and I would appreciate input on how to achieve this.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I think this could help you, it works with just parts of those names too:

const typedWord = "DC"; 
const list = [{name: "Batman", company: "DC"}, {name: "Spiderman", company: "Marvel"}]

const filteredList = list.filter(function (object) {
    const entries = Object.entries(object).map((entry) => { return entry[0] })
    var matches = entries.filter((entry) => { return object[entry].includes(typedWord) })
    return matches.length !== 0
})
about 4 years ago · Santiago Trujillo Relatório

0

It seems like you're searching for a way to look at all of the values within a given object. This would be a great place to use Object.values(), which returns an array of each value in the object, so your first object would look like:

["Batman", "Bruce Wayne", "DC"]

Great! Now if you have a search term, let's call it lookup for ease of examples, you can loop through (or use the .includes() JS functionality like you did) this array you create with Object.values() like so:

const result = heroes.filter(each => {
    let objectValues = Object.values(each);

    return objectValues.includes(lookup);
})

I took out some portions of your code like the greater function of this snippet and how you are making doing the comparisons with lowercased strings, but I leave that as a challenge.

about 4 years ago · Santiago Trujillo Relatório

0

Try this:

function search(lookup){
    return heros.filter(hero =>{
           return Object.values(hero).filter(value => value.includes(lookup))
        })
   }
about 4 years ago · Santiago Trujillo 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