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

79
Visualizações
Filter array of multiple value

This is my issue, I try to get result of items base on my filter array.

const papaFilter=[
{type:'event', value: ['In Person']},
{type:'city', value: ['Boston', 'Miami', 'New York']}
]

const items = [
{city: 'Boston', type: 'In Person'},
{city: 'New Jersey', type: 'In Person'},
{city:'Boston', type:'Virtual'}
]

const filteredResults = items.filter(el => papaFilter.some(filterEl => el[filterEl.type] === filterEl.value));

I want the first object of my items because papafilter contain Boston and In Person.

I know in my condition filterEl.value is an array of multiple value so this condition doesn't work. Any Ideas?

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

0

You could filter the array checking all filter item.

This approach takes an object for replacing keys.

const
    replacements = { event: 'type' },
    filter = [{ type: 'event', value: ['In Person'] }, { type: 'city', value: ['Boston', 'Miami', 'New York'] }],
    items = [{ city: 'Boston', type: 'In Person' }, { city: 'New Jersey', type: 'In Person' }, { city: 'Boston', type: 'Virtual' }],
    result = items.filter(o =>
        filter.every(({ type, value }) => value.includes(o[(replacements[type] || type)]))
    );

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are looking for Array.includes. So, filterEl.value.includes(el[filterEl.type]) in the last block should give you the desired result.

You can find more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

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