如何使用具有多个值的单个KEY过滤数组?

时间:2019-03-30 17:32:17

标签: javascript arrays reactjs

const usersData = [
{
  
  "count": 10,
  "customerList": [
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Family Practice",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "General Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Internal Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Internal Medicine",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Family Practice",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "F",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    },
    {
      "primarySpecialty": "Multi-Specialty Grp",
      "primarySpecialtyCode": "008",
      "gender": "M",
      "graduationYear": 2001,
      "regularOfficeHours": true,
      "extendedOfficeHours": true
    }
    
  ]
}
]

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']
let filteredProviderData = usersData[0].customerList.filter(function(e) {
  return filterKeyName.every(function(a) {
    return filterValue.includes(e[a])
  })
})
console.log(filteredProviderData)

这里有一个供用户使用的样本数据。 在这里,我的要求是使用多个值过滤键。 一旦您使用filterKeyName和filterValue运行代码,如下所示

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']

您将获得如此处显示的输出。

[
  {
    "primarySpecialty": "Family Practice",
    "primarySpecialtyCode": "008",
    "gender": "M",
    "graduationYear": 2001,
    "regularOfficeHours": true,
    "extendedOfficeHours": true
  }
]

这里我的要求是过滤具有多个值的单个键 if primarySpecialty = ['Family Practice','General Medicine']Gender = ['F','M']如何过滤用户代码,如代码段所示。

谢谢。

1 个答案:

答案 0 :(得分:2)

您应该使用当前键及其索引,以便进行所需的比较:

return e[k] === filterValue[i];

const usersData = [{    "count": 10,  "customerList": [    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Family Practice",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "General Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Internal Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Internal Medicine",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Family Practice",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "F",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    },    {      "primarySpecialty": "Multi-Specialty Grp",      "primarySpecialtyCode": "008",      "gender": "M",      "graduationYear": 2001,      "regularOfficeHours": true,      "extendedOfficeHours": true    }      ]}]

let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']
let filteredProviderData = usersData[0].customerList.filter(function(e) {
  return filterKeyName.every(function(k, i) {
    return e[k] === filterValue[i];
  })
})
console.log(filteredProviderData);
.as-console-wrapper { max-height: 100% !important; top: 0; }