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

117
Visualizações
Filter an array of objects based on another array

I want to filter this array of Object:

const testArray = [{
  id: 4,
  filters: ["Norway", "Sweden"]
}, {
  id: 2,
  filters :["Norway", "Sweden"]
}, {
  id: 3,
  filters:["Denmark", "Sweden"]
}]

with the filter

const myFilter=["Norway", "Sweden"]

However my code just returns []? What I have tried so far:

const testArray = [{
  id: 4,
  filters: ["Norway", "Sweden"]
}, {
  id: 2,
  filters :["Norway", "Sweden"]
}, {
  id: 3,
  filters:["Denmark", "Sweden"]
}]
const myFilter=["Norway", "Sweden"]

console.log(testArray.filter(e=>e.filters===myFilter))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use Array#every to test the equality+ of two arrays as follows:

const output = testArray.filter(
    e => e.filters.every(
        filter => myFilter.includes(filter)
    )
);

const 
testArray = [{ id: 4, filters: ["Norway", "Sweden"] }, { id: 2, filters :["Norway", "Sweden"] }, { id: 3, filters:["Denmark", "Sweden"] }],
myFilter = ["Norway", "Sweden"],

output = testArray.filter(
    e => e.filters.every(
       filter => myFilter.includes(filter)
    )
);

console.log(output)

NOTE: Array#every as used here just confirms that every element of each array is in the other, regardless of the order in which the elements appear.

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