gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:05:42 25 4
gpt4 key购买 nike

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'] 如何过滤代码中显示的用户数据片段。

提前致谢。

最佳答案

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

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

关于javascript - 如何使用具有多个值的单个 KEY 过滤数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com