JSON按时间过滤

时间:2018-06-09 07:15:42

标签: javascript json

我有像这样的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以使用" Start_time "中午12点过后?我正在考虑做像

这样的事情
let filtered_data = data.filter(o => o.Start_time >= '12 PM');

但当然这不会起作用" Start_time"是'字符串'而不是' date'类型。

1 个答案:

答案 0 :(得分:3)

你没有JSON - 你有一个对象数组。 JSON是一种格式化字符串以表示对象的特殊方式

幸运的是,在您的情况下,应该非常容易 - 在中午12点之后过滤事件,您只需检查Start_time属性以查看字符串是否包含PM



const input=[{"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"}];

console.log(
  input.filter(({ Start_time }) => Start_time.includes('PM'))
);




请注意,includes是ES6函数,您可能需要polyfill