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

106
Visualizações
filtering with multiple conditions in javascript

I am trying to filter data based on input and it works fine but I want add an extra condition to it. It should return if titel or another value (for example sendFrom) is given.

 const newData = masterDataSource.filter(function (item) {

    const itemData = item.titel  ? item.titel.toUpperCase() : ''.toUpperCase();
    const textData = text.toUpperCase();
    return itemData.indexOf(textData) > -1;
  });

I tried adding some operators but it didn´t worked out as planed

     const newData = masterDataSource.filter(function (item) {

    const itemData = item.titel || item.sendFrom ? item.titel.toUpperCase() || item.sendFrom.toUpperCase() : ''.toUpperCase();
    const textData = text.toUpperCase();
    return itemData.indexOf(textData) > -1;
  });

thanks

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

0

If I understand what you're after you would want something like:

const itemData = item.titel ? item.titel.toUpperCase() : (item.sendFrom ? item.sendFrom.toUpperCase() : ''.toUpperCase());

Parentheses are not required, but I added them to make it easier to get the logic.

Also, ''.toUpperCase() doesn't make much sense, it's the same as ''.

You could replace the initial condition with

const itemData = (item.titel  && item.titel.toUpperCase()) || '';

and the revised version:

const itemData = (item.titel  && item.titel.toUpperCase()) || (item.sendFrom  && item.sendFrom.toUpperCase()) || '';

Again, parentheses are not required.

References:
Conditional (ternary) operator
Logical Operators

about 4 years ago · Juan Pablo Isaza Relatório

0

You could collect the wanted properties in an array and iterate them while checking the values.

const newData = masterDataSource.filter(({ titel = '', sendFrom = '' }) => 
    [titel, sendFrom].some(s => s.toUpperCase().includes(text.toUpperCase()))
);
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