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

120
Views
Filtering on multiple conditions using javascript filter

Before anyone calls duplicate please read it through first...

I was trying to use the filter function with multiple conditions but doesn't seem to work and alot of solutions to this roughly come out to say I can do...

const data = [
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6}
]

data.filter(f=>f.SectionID == 6 && f.SectionID == 0 && f.CatalogID == -1)

let a = data.filter(function(e) {
        return e.SectionID == 6 && e.SectionID == 0 && e.CatalogID == -1
    });
console.log(a);

But those return empty... But when I loop through the data object, like this...

let holder = [];
for(let i = 0; i < data.length; i++){
    if(data[i].SectionID === 6 || data[i].SectionID === 0 || data[i].CatalogID === -1) holder.push(data[i]);
}
console.log(holder);
holder = [];

Then it does the job, but its not pretty.

Is there something glaringly obvious that is wrong with the code I provided, that is not popping out at me?

** EDIT ** The result I am looking for is this...

{CatalogID: -1, SectionID: 0},
{CatalogID: -1, SectionID: 0},
{CatalogID: -1, SectionID: 0},
{CatalogID: 3, SectionID: 6},
{CatalogID: 3, SectionID: 6},
{CatalogID: -1, SectionID: 0},
{CatalogID: 3, SectionID: 6}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

f.sectionID = 6 && f.sectionID == 0 can't ever be true -- there can only be one sectionID value in a specific object f.

You need to use || to match different values of the same sectionID property. Then use && to combine condition with catalogID.

const data = [
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: 2, SectionID: 9},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: 3, SectionID: 6},
    {CatalogID: -1, SectionID: 0},
    {CatalogID: 3, SectionID: 6}
]

let a = data.filter(f => f.SectionID == 6 || ( f.SectionID == 0 && f.CatalogID == -1))

console.log(a);

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!