JS数组:标记重复项 [英] JS Array: tagging duplicates
本文介绍了JS数组:标记重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的对象数组...
I have an array of objects like this ...
[{
"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"
}]
我正在寻找一种针对开始时间"冲突的过滤器.我的预期输出应包含一个额外的键值对,例如"clash":"Yes".因此,预期输出应为..
I am looking to implement a filter for 'Start_time' clash. My expected output should contain an extra key value pair something like "clash": "Yes". So the expected output should be ..
[{
"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)之后,而不是第一次出现.我已经在此处看到了类似的解决方案,但这并没有标记重复项,而是删除了重复项.
Notice the extra "key/value" pair appears on the 1st clash (9:00 AM) onwards, not on the initial occurrence. I have seen a similar solution here but this doesn't tag duplicates, it rather removes them.
非常感谢.
推荐答案
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);
这篇关于JS数组:标记重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文