gpt4 book ai didi

javascript - JSON 中的条件过滤

转载 作者:行者123 更新时间:2023-11-30 09:22:37 24 4
gpt4 key购买 nike

我有这个 JSON 文件 ...

[{
"Event_code": "AB-001",
"Interest_area": "Arts and Education",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-002",
"Interest_area": "Arts and Education",
"Start_time": "12:30 PM",
"End_time": "1:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-003",
"Interest_area": "",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-004",
"Interest_area": "Business",
"Start_time": "10:30 AM",
"End_time": "11:00 AM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-005",
"Interest_area": "General Interest",
"Start_time": "9:30 AM",
"End_time": "1:30 PM",
"Session_type": "Experience"
}, {
"Event_code": "AB-006",
"Interest_area": "Environment , Business ",
"Start_time": "11:00 AM",
"End_time": "11:30 AM",
"Session_type": "Course information session"
}]

我想过滤此 JSON,其中“Session_type”等于“Course information session”,“Interest_area”等于“业务

到目前为止我已经尝试过......

let search = 'Course information session';
let result = [...new Set(arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' && o.Interest_area === "Business"))]
console.log(result);

此代码仅返回事件代码为“AB-004”而非“AB-006”的事件我想同时返回 AB-004 和 AB-006

有人可以告诉我我做错了什么吗?谢谢

最佳答案

使用String.includes

let arr = [{ "Event_code": "AB-001", "Interest_area": "Arts and Education", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-002", "Interest_area": "Arts and Education", "Start_time": "12:30 PM","End_time": "1:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-003", "Interest_area": "", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-004", "Interest_area":"Business", "Start_time": "10:30 AM", "End_time": "11:00 AM", "Session_type": "Course information session" }, { "Event_code": "AB-005", "Interest_area": "General Interest", "Start_time": "9:30 AM", "End_time": "1:30 PM", "Session_type": "Experience" },{ "Event_code": "AB-006", "Interest_area": "Environment , Business ", "Start_time": "11:00 AM", "End_time": "11:30 AM", "Session_type": "Course information session" }];

let search = 'Course information session', interestArea = "Business";
let result = arr.filter(o=> o.Session_type === search && o.Interest_area.includes(interestArea));

console.log(result);

注意:Set只有在你想删除重复项时才使用,但是,数组中没有重复项,因此你可以避免使用它。

关于javascript - JSON 中的条件过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50596388/

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