簡體   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