JSON中的条件过滤

时间:2018-05-30 04:26:15

标签: javascript jquery

我有这个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 "等于" 课程信息会话"和" 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);

此代码仅返回Event_code' AB-004'而不是' AB-006' 我想要返回AB-004和AB-006

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

3 个答案:

答案 0 :(得分:2)

使用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);




注意:仅当您要删除重复项时才使用设置,但是,数组中没有重复项,因此,您可以避免使用它。

答案 1 :(得分:1)

使用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" }],
    search = 'Course information session',
    result = arr.filter(o => o.Session_type === search && o.Interest_area.trim() !== '' && o.Interest_area.includes("Business"));
console.log(result);




答案 2 :(得分:1)

AB-006

上查看Interest_area
    "Interest_area": "Environment ,    Business       ",

Interest_area ===“商业”总是假的。试试这个:

Interest_area.includes("Business")

将返回true。