Home > database >  How do I filter an array object with multiple filterBy values?
How do I filter an array object with multiple filterBy values?

Time:01-10

I am trying to filter an array object with array value.

const arrayData = [
  {
    name: 'abc',
    designation: 'senior',
    phone: '3589234556',
    address: 'Address 1'
  },
  {
    name: 'pqr',
    designation: 'junior',
    phone: '',
    address: 'Address 2'
  },
  {
    name: 'xyz',
    designation: 'senior',
    phone: '3589234557',
    address: ''
  }
]

const filterBy = ['senior','junior', 'missing_contact_info']

Note: 'missing_contact_info' means if the 'phone' or 'address' is empty or null

Expected conditions:-

condition1: senior

condition2: senior junior

condition3: senior junior missing_contact_info

condition4: senior missing_contact_info

. . . etc

CodePudding user response:

this is my solution to your problem

const subresult = arrayData.filter(c=>filterBy.includes(c.designation));

var result;
if (filterBy.includes('missing_contact_info')) {
    result = subresult.filter(c=> !c.phone || !c.address);
}
else 
{
   result = subresult;
}
  • Related