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)
Here there is a sample data for the users. Here my requirement is to filter the key with multiple values. here once you run the code using filterKeyName and filterValue as shown below
let filterKeyName = ['gender','regularOfficeHours','primarySpecialty']
let filterValue = ['M',true,'Family Practice']
you will get the output as displayed here.
[
{
"primarySpecialty": "Family Practice",
"primarySpecialtyCode": "008",
"gender": "M",
"graduationYear": 2001,
"regularOfficeHours": true,
"extendedOfficeHours": true
}
]
Here my requirement is to filter single key with multiple values means
if primarySpecialty = ['Family Practice','General Medicine'] and Gender = ['F','M'] how to filter the usersData as shown into the code snippet.
Thanks in Advance.
.indexOfexists) in a function that you use withusersData.customerList.filter(...)to decide whether an object matches or not? That should be more than enough of a hint for you to write the rest of the code yourself.