- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的模拟数据:
[
{
"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/
我有如下功能 var resultObject = { testResult: $('.test').map(function() { return {
我的模拟数据: [ { "id": 1, "first_name": "Suzy", "last_name": "Pinnell", "email": "spinn
每个方法有多个 try{}catch{} 语句是一种好习惯吗? 最佳答案 在我看来,让每个方法只处理一个任务是一种很好的做法。因此,您很少需要在一个方法中使用多个 try/catch block 。但
Linux,C.我创建了多个线程来运行工作负载,我想在指定的秒数/超时后通知这些线程停止/终止。我如何用 C 实现它? void *do_function(void *ptr) { //cal
我需要能够创建一个表单,其中有几个 text_field_tags 根据另一个模型的实例动态创建。我希望能够将输入到这些 text_field_tags 的值存储到同一个数组中,一旦提交表单,我就可以
背景:我专门为 ASP.NET 应用程序使用存储过程。我正在使用 DataReader 加载数据集对象。 最佳答案 单个过程返回单个结果(为了代码的可维护性、简单性和过程的重用),然后一次调用调用所有
我是一名优秀的程序员,十分优秀!