Home > database >  Global filter on array of objects in JavaScript based on multiple keywords
Global filter on array of objects in JavaScript based on multiple keywords

Time:04-27

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.

CodePudding user response:

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);

  • Related