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

115
Views
How to filter collection with multiple conditions (strings and array of values) using lodash javascript

I'm trying to filter a collection with multiple conditions using lodash, it works fine with strings, but I want to filter by an array of id's for the part of the locations and string for status. I have tried to change the id to array of id's "location":{"id" :["1","4"]}, but it doesn't work

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

 let filterByConditions = {
  "location":{"id" :"1"},
  "account":{ "id": "1"},
  "status": "ok",
};

let results = _.filter(collection, filterByConditions);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It looks like you're trying to use the filter method with the "matches" shorthand. This shorthand doesn't support an "or" condition; only exact matches. However, you can pass the _.filter method a predicate instead, and it'll work:

const collection = [ 
  {account: {id: "1", name: "a"}, location:{id: "1", name: "x"}, status: "ok"},
  {account: {id: "2", name: "b"}, location:{id: "2", name: "x"}, status: "ok"},
  {account: {id: "3", name: "c"}, location:{id: "4", name: "y"}, status: "failed"},
  {account: {id: "4", name: "d"}, location:{id: "4", name: "y"}, status: "ok"},]

let filterByPredicate = (elem) => {
  return elem.status === "ok" && 
    elem.location.id === "1" && 
    (elem.account.id === "1" || elem.account.id === "4");
};

let results = _.filter(collection, filterByPredicate);
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!