gpt4 book ai didi

对象 : filter by multple AND conditions 的 Javascript 数组

转载 作者:行者123 更新时间:2023-12-05 01:54:53 24 4
gpt4 key购买 nike

我的模拟数据:

[
{
"id": 1,
"first_name": "Suzy",
"last_name": "Pinnell",
"email": "spinnell0@utexas.edu",
"gender": "Agender",
"image": "http://dummyimage.com/410x239.png/5fa2dd/ffffff",
"department": "Marketing",
"job_title": "Quality Control Specialist",
"skill": "Residential Homes"
},
{
"id": 2,
"first_name": "Enriqueta",
"last_name": "Folbig",
"email": "efolbig1@google.com.br",
"gender": "Male",
"image": "http://dummyimage.com/247x244.png/5fa2dd/ffffff",
"department": "Sales",
"job_title": "Environmental Specialist",
"skill": "MMC"
},
{
"id": 3,
"first_name": "Simmonds",
"last_name": "Acomb",
"email": "sacomb2@amazon.co.uk",
"gender": "Polygender",
"image": "http://dummyimage.com/315x256.png/dddddd/000000",
"department": "Human Resources",
"job_title": "Accountant",
"skill": "Xilinx"
},
{
"id": 4,
"first_name": "Bernita",
"last_name": "Hartman",
"email": "bhartman3@whitehouse.gov",
"gender": "Female",
"image": "http://dummyimage.com/305x275.png/dddddd/000000",
"department": "Support",
"job_title": "Account ExecutiveII",
"skill": "Airframe"
}
]

我在这里发现了一个类似的问题: javascript filter array multiple conditions

要定义要过滤的内容,他们会传递一个对象:

var filter = {
gender: 'male',
department: 'Sales'
};

这行得通。它显示了包含这两个键的每个对象。

+我试图修改它,所以我可以使用一个数组:*

var filter = {
gender: ['Male'],
department: ['Sales', 'Marketing']
};

我的目标是根据数组中的给定值过滤数据。只应出现项目,其中包含销售和市场营销中的所有男性。

我试过包含(内部过滤器和 for in 循环),但它只显示第一个数组的结果,第二个被忽略。我不确定如何在数组内为所有这些实现 AND 条件。

谢谢。

最佳答案

您可以提前存储所有条目,以防止获取每个数据对象以获取过滤器的相同条目数组。

过滤条目以删除可能的空数组。

然后获取条目并检查值是否是数据之一,或者过滤器是否包含数组,然后检查过滤器数组是否包含数据中的值。

const
data = [{ id: 1, first_name: "Suzy", last_name: "Pinnell", email: "spinnell0@utexas.edu", gender: "Agender", image: "http://dummyimage.com/410x239.png/5fa2dd/ffffff", department: "Marketing", job_title: "Quality Control Specialist", skill: "Residential Homes" }, { id: 2, first_name: "Enriqueta", last_name: "Folbig", email: "efolbig1@google.com.br", gender: "Male", image: "http://dummyimage.com/247x244.png/5fa2dd/ffffff", department: "Sales", job_title: "Environmental Specialist", skill: "MMC" }, { id: 3, first_name: "Simmonds", last_name: "Acomb", email: "sacomb2@amazon.co.uk", gender: "Polygender", image: "http://dummyimage.com/315x256.png/dddddd/000000", department: "Human Resources", job_title: "Accountant", skill: "Xilinx" }, { id: 4, first_name: "Bernita", last_name: "Hartman", email: "bhartman3@whitehouse.gov", gender: "Female", image: "http://dummyimage.com/305x275.png/dddddd/000000", department: "Support", job_title: "Account ExecutiveII", skill: "Airframe" }],
filter = { last_name: [], gender: ['Male'], department: ['Sales', 'Marketing'] },
filterEntries = Object
.entries(filter)
.filter(([, v]) => !Array.isArray(v) || v.length),
result = data.filter(o => filterEntries.every(([k, v]) =>
o[k] === v ||
Array.isArray(v) && v.includes(o[k])
));

console.log(result);

关于对象 : filter by multple AND conditions 的 Javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70475718/

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