繁体   English   中英

JSON按时间过滤

[英]JSON filtering by time

我有这样的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以在中午12点之后使用“ Start_time ”获取事件? 我在想做类似的事情

let filtered_data = data.filter(o => o.Start_time >= '12 PM');

但是当然这不起作用,因为“Start_time”是'string'而不是'date'类型。

你没有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功能,您可能需要填充

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM