javascript 如何过滤具有多个filterBy值的数组对象?

lf5gs5x2  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(113)

我正在尝试使用数组值筛选数组对象。

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']

注意:“missing_contact_info”表示“电话”或“地址”是否为空
预期条件:-
条件1:senior
条件2:senior+junior
条件3:senior+junior+missing_contact_info
条件4:senior+missing_contact_info
等等

roqulrg3

roqulrg31#

这是我解决你问题的办法

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;
}

相关问题