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

194
Visualizações
How to make filter function that doesn't stop at first false?

Im trying to filter an array of objects by comparing it to another array by looping through it's values. The problem is that the filter function stops at the first false by default, so if the loop returns (false, true, false) it will not pass the filter.

is there a way around this?

code (I've put simplfied arrays and objects in the code for the example):

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(function(el, i) {
    do {
        return el.location === filteredLocations[i];
    }
    while (filteredLocations.length >= i)
})
// this only returns the first object instead of the desired first and third

Thank you!

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

0

Use the .filter function as it is intended, any return will be added to the returned array.

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(el => {
    return filteredLocations.includes(el.location);
});

sidenote, your code has 2 issues:

  1. it may return false inside the while loop, if it doesnt match the 1st occurrence, hence why the single output you had
  2. you compare filteredLocations.length with the i (index) of the jobs array loop, jobs and filteredLocations are 2 different arrays, so that doesnt make sense to do so
about 4 years ago · Santiago Gelvez Relatório

0

Filter doesn't stop at first false. It works correctly but it cannot return the first and third objects because the third object is on index 2 and you don't have any items in filteredLocations on that index.

about 4 years ago · Santiago Gelvez Relatório

0

I would advise you to replace the do-while loop with a simple finder function as follows:

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(function(el, i) {
    return filteredLocations.find((filteredLocation) => filteredLocation === el.location);
})
console.log(filteredJobs);

about 4 years ago · Santiago Gelvez 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