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

99
Visualizações
ignore a object property while filtering is empty

I am trying to filter object array based on user selected value from drop down list

As you see in below example I want to filter the array based on chain and officeid. If one of them is empty then I want to skip that filter.

let list = [{
      "index": 4,
      "chain": "aaa",
      "officeId": "bbb"
    },
    {
      "index": 5,
      "chain": "ccc",
      "officeId": "ddd"
      
    }];

function search(chain, office) {
 let data = list.filter(function(result) {
      return result.chain === chain && result.officeId === office;
 });
 
 console.log(data);
}

search('aaa','bbb');  // this will work fine
search('aaa',''); // this returns empty array
search('', 'bbb'); // empty array

In above 3 statements I am expecting same output for all

If both are empty then it should skip the filter and return original array

i.e search('', ''); should not apply any filters

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

0

try using this instead, you just have to add one more condition

let list = [{
      "index": 4,
      "chain": "aaa",
      "officeId": "bbb"
    },
    {
      "index": 5,
      "chain": "ccc",
      "officeId": "ddd"
      
    }];

function search(chain, office) {
 let data = list.filter(function(result) {
      if( result.chain === chain && result.officeId === office){
         return result
      }
      if(!chain  && !office){
         return result;
      }
 });
 
 console.log(data);
}

search('aaa','bbb');  // this will work fine
search('aaa',''); // this returns empty array
search('', 'bbb'); // empty array
search("","");

You can also use a short hand like this

 let data = list.filter(function(result) {
      return (!chain && !office) || (result.chain === chain && result.officeId === office);
 });
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