如何对JSON数组添加条件筛选,提取唯一Interest_area值?
Got it, let's work through this problem since the previous jQuery-based approach doesn't handle the multi-value Interest_area entries. Here's a step-by-step solution using vanilla JavaScript that'll get you the expected result (and even handles edge cases like empty fields or extra spaces):
Step 1: Filter Relevant Events
First, we'll narrow down the list to only entries where Session_type equals "Course information session". This excludes any events that don't fit our criteria (like the "Experience" session in your data).
Step 2: Split & Clean Multi-Value Areas
Next, we need to handle those Interest_area fields that have multiple values separated by commas. We'll split each field into individual areas, trim any extra whitespace, and skip empty fields entirely.
Step 3: Get Unique Values
Finally, we'll remove duplicates using a Set (which automatically handles uniqueness) and convert it back to an array. Optionally, we can sort the array to match your expected output order.
Full Code Example
const events = [ { "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" }, { "Event_code": "AB-014", "Interest_area": "Health sciences and allied health, Medicine", "Start_time": "1:00 PM", "End_time": "2:00 PM", "Session_type": "Course information session" } ]; // Filter to keep only course info sessions const filteredEvents = events.filter(event => event.Session_type === "Course information session"); // Split multi-value areas, clean up whitespace, skip empty entries const allInterestAreas = filteredEvents.flatMap(event => { if (!event.Interest_area) return []; return event.Interest_area.split(',').map(area => area.trim()).filter(area => area.length > 0); }); // Get unique values and sort to match expected order const uniqueAreas = [...new Set(allInterestAreas)].sort(); console.log(uniqueAreas); // Output: ["Arts and Education", "Business", "Environment", "Health sciences and allied health", "Medicine"]
Why This Works Where Previous Approaches Didn't
The earlier solution you referenced only checked for uniqueness on the full Interest_area string. But since some entries have multiple areas combined (like "Environment, Business"), we needed to split those into individual values first before checking for duplicates. This approach ensures every single interest area is captured and deduplicated correctly.
内容的提问来源于stack exchange,提问作者Slyper

