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

383
Visualizações
Global filter on array of objects in JavaScript based on multiple keywords

I'm trying to filter an array of objects based on multiple search keywords.

I'm able to filter the array based on OR(||) condition. But I'm not sure on how to filter based on AND(&&) condition.

here is what I've tried

let aList = [
  {
    "id": 14,
    "name": "Summer season",
    "start_time": "01-03-2022",
    "end_time": "31-03-2022",
  },
  {
    "id": 39,
    "name": "winter",
    "start_time": "01-05-2022",
    "end_time": "17-05-2022",
  },
  {
    "id": 40,
    "name": "winter season",
    "start_time": "18-05-2022",
    "end_time": "31-05-2022",
  }
];
let filterProperty = ["name","start_time","end_time"];
let searchKeyword = ["winter","18"];

function search(array, textArray, filterColumns) {
  textArray = textArray.map(t => t.toLowerCase());
  return array.filter((a) => {
    return filterColumns.some((f) => {
      return textArray.some((t) => {
        return a[f].toString().toLowerCase().indexOf(t) !== -1;
      });
    });
  });
}

let result = search(aList, searchKeyword, filterProperty);
console.log(result);

I'm getting the following output (OR condition)

[
  {
    "id": 39,
    "name": "winter",
    "start_time": "01-05-2022",
    "end_time": "17-05-2022"
  },
  {
    "id": 40,
    "name": "winter season",
    "start_time": "18-05-2022",
    "end_time": "31-05-2022"
  }
]

I need output like the one below as result should be based on both '18' and 'winter' (AND condition),

[  
  {
    "id": 40,
    "name": "winter season",
    "start_time": "18-05-2022",
    "end_time": "31-05-2022"
  }
]

This may be a duplicate question, but I didn't find any similar questions in this forum. I've been stuck on this for sometime. can anyone help?. Is there any other or better way to achieve a solution to this problem.

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

0

Basically rather than Array.some you use Array.every, when checking through the searchKeyword array.

I switched the positions of filterColumns.some and textArray.every to make the logic work.

let aList = [
  {
    "id": 14,
    "name": "Summer season",
    "start_time": "01-03-2022",
    "end_time": "31-03-2022",
  },
  {
    "id": 39,
    "name": "winter",
    "start_time": "01-05-2022",
    "end_time": "17-05-2022",
  },
  {
    "id": 40,
    "name": "winter season",
    "start_time": "18-05-2022",
    "end_time": "31-05-2022",
  }
];
let filterProperty = ["name","start_time","end_time"];
let searchKeyword = ["winter","18"];

function search(array, textArray, filterColumns) {
  textArray = textArray.map(t => t.toLowerCase());
  return array.filter((a) => {
    return textArray.every((t) => {
      return filterColumns.some((f) => {    
        return a[f].toString().toLowerCase().indexOf(t) !== -1;
      });
    });
  });
}

let result = search(aList, searchKeyword, filterProperty);
console.log(result);

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