Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

385
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda