我有一个像这样的对象数组......
[{
"Event_code": "AB-001",
"Interest_area": "Arts",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-002",
"Interest_area": "Arts",
"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:00 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"
}]
我希望为'Start_time'碰撞实现一个过滤器。我的预期输出应包含一个额外的键值对,如“clash”:“是”。所以预期的输出应该是..
[{
"Event_code": "AB-001",
"Interest_area": "Arts",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-002",
"Interest_area": "Arts",
"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",
"clash": "yes" // Newly added key/value
}, {
"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:00 AM",
"End_time": "1:30 PM",
"Session_type": "Experience",
"clash": "yes" // Newly added key/value
}, {
"Event_code": "AB-006",
"Interest_area": "Environment , Business ",
"Start_time": "11:00 AM",
"End_time": "11:30 AM",
"Session_type": "Course information session"
}]
请注意,额外的“键/值”对出现在第一次碰撞(上午9:00)之后,而不是在最初发生时。我已经看到了类似的解决方案here但是这并没有标记重复,而是删除了它们。
非常感谢提前。
答案 0 :(得分:2)
使用 if / else
var arr = [{"Event_code":"AB-001","Interest_area":"Arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-002","Interest_area":"Arts","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:00 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"}];
var st = {};
arr.forEach(o => {
if(st[o.Start_time]) o.clash = "yes";
else st[o.Start_time] = o.Start_time;
});
console.log(arr);

使用三元运算符
var arr = [{"Event_code":"AB-001","Interest_area":"Arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-002","Interest_area":"Arts","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:00 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"}];
var st = {};
arr.forEach(o => st[o.Start_time] ? o.clash = "yes": st[o.Start_time] = o.Start_time);
console.log(arr);