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

83
Views
filter array based on array of multiple condition

I Would like to return the two last object based on the filterArray because filterArray contain 'Orange' as tags and 'Fruit' as type. but because all my obj contain an array of tags the function includes doesn't work. (works only if the value tags is a string).

const filterArray = [
          { type: 'type', value: ['Fruit'] },
          { type: 'tags', value: ['Apple', 'Orange', 'Peach'] },
        ]
    const obj = [
      { type: 'Sweet Fruit', tags: ['Apple'] },
      { type: 'Fruit', tags: ['Orange', 'Watermelon'] },
      { type: 'Fruit', tags: ['Orange'] },
    ]
    const Newresult = obj.filter(item =>
      filterArray.every(({ type, value }) => value.includes(item[type])),
    )

    console.log(Newresult)

this function works if the const obj.tags is not an array but string.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use the following:

// use a set in the filter so lookups happen in O(1) 
const filter = [
  { type: "type", value: new Set(["Fruit"]) },
  { type: "tags", value: new Set(["Apple", "Orange", "Peach"]) },
];

const obj = [
  { type: "Sweet Fruit", tags: ["Apple"] },
  { type: "Fruit", tags: ["Orange", "Watermelon"] },
  { type: "Fruit", tags: ["Orange"] },
];

const Newresult = obj.filter((item) => (
  filter.every(({ type, value }) => {
    // check whether the value we want to filter by is an arry
    if(Array.isArray(item[type])){
      // yes, we are dealing with an array => check if any of the filter criteria are matched
      // if you want to check for all criteria use "every()" instead of "some"
      return item[type].some(valueInArray => value.has(valueInArray));
    }
    // if we are not dealing with an array we need to just check one value
    return value.has(item[type]);
  })
));

console.log(Newresult);

Key changes to your approach:

  • I check whether I am dealing with an Array and if I do, I check if any of the values within the array is within the whitelist values provided by the filter
  • I use a Set instead of an array which reduces the time for lookups O(n) for arrays to 0(1) and therefore significantly speeds up the algorithm.
about 4 years ago · Juan Pablo Isaza Report

0

You could treat every value of data as array and check if the data contians a value from the filter.

const
    filter = [{ type: 'type', value: ['Fruit'] }, { type: 'tags', value: ['Apple', 'Orange', 'Peach'] }],
    data = [{ type: 'Sweet Fruit', tags: ['Apple'] }, { type: 'Fruit', tags: ['Orange', 'Watermelon'] }, { type: 'Fruit', tags: ['Orange'] }, ],
    result = data.filter(o => filter.every(({ type, value }) => []
        .concat(o[type])
        .some(v => value.includes(v))
    ));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!