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

64
Views
filter array of objects if properties are in another array and values are true

The following is a sample that yields the desired output.

const data = [
                { itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
                { itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
                { itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
                { itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
                { itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
            ];
           
document.getElementById("data").innerText = JSON.stringify(data.filter(o => o.inventoryFlag || o.donateFlag));
<textarea id="data" rows="50" cols="100"></textarea>

I'm trying to find a way to list the filter-by flags in a separate array and then use that in the .filter() somehow to get the same result set as the above code.

For example, here are the flags to use for filtering AND at least one of the values must be true in the object to be included in the final dataset -

const flags = ['inventoryFlag', 'donateFlag'];

As a start, I tried this but it didn't do anything:

const filteredData = data.filter(o => flags.includes(Object.keys(o)));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Inside of the filter, use the some method to detect if any of the property keys of an entry, that are contained in the flags array, have the a value of true.

const data = [
  { itemID: '300', status: 'active', inventoryFlag: true, certifiedFlag: false, donateFlag: true },
  { itemID: '400', status: 'inactive', inventoryFlag: true, certifiedFlag: true, donateFlag: false },
  { itemID: '500', status: 'active', inventoryFlag: false, certifiedFlag: false, donateFlag: false },
  { itemID: '600', status: 'active', inventoryFlag: false, certifiedFlag: true, donateFlag: true },
  { itemID: '700', status: 'inactive', inventoryFlag: false, certifiedFlag: true, donateFlag: false }
];

const flags = ['inventoryFlag', 'donateFlag'];

const result = data.filter(entry => 
  flags.some(flag => entry[flag] === true)
);

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!