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

66
Visualizações
filter return combined matches
arr = [{name: "tom", tools:["spanner", "hammer", "screwdriver"]}, 
{name: "john", tools:["Mallet", "hammer", "screwdriver"]},
{name: "phil", tools:["Mallet", "Drill", "screwdriver"]}]


private filterObjectArray(arr: any, filterArr: any): any {
  return arr.filter((el: any) =>
    filterArr.some(
      (f: any) =>
        el.tools.toString()?.toLowerCase().indexOf(f.name?.toLowerCase()) >= 0
    )
  );
 }

My filter is similar to the above, at the moment if my filter returned filterArr = [{name: "screwdriver"}, {name: "hammer"}] then all objects would be returned as they all have at least one match in screwdriver.

However, how do I make it that only objects that has both filter properties display e.g. in the above I expect only the two objects tom and john return as they both have a screwdriver and hammer, whereas phil only has a screwdriver .

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Here is a solution in JS. You can use every here to check if that every filter is included in the tools array

let arr = [{name: "tom", tools:["spanner", "hammer", "screwdriver"]}, 
{name: "john", tools:["Mallet", "hammer", "screwdriver"]},
{name: "phil", tools:["Mallet", "Drill", "screwdriver"]}]

let filterArr = [{name: "screwdriver"}, {name: "hammer"}]

let res = arr.filter(({tools}) => filterArr.every(({name}) => tools.includes(name)))

console.log(res)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use every instead of the includes(index >= 0)

It means

everything match then true otherwise false

private filterObjectArray(arr: any, filterArr: any): any {
    return arr.filter((el: any) => filterArr.every((f: any) => el.tools.includes(f.name)))
}
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