Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

382
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!