我有一个像这样的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"
},
{
"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"
}
]
我想要做的是过滤此JSON并提取唯一“ Interest_area ”值,其中“ Session_type ”等于“课程信息会议“......
我的预期输出
[“艺术与教育”,“商业”,“环境”]
我看过this解决方案,这与我正在寻找的解决方案非常接近,但它在我的情况下不起作用,因为我的JSON可能有“2个或更多”值的“兴趣区域“字段。
答案 0 :(得分:2)
首先使用Array#filter()
进行过滤,以获得非空兴趣和适当的会话类型
Array#reduce()
将结果过滤为感兴趣的Set
。集合只能具有唯一值并忽略重复
最后将Set转换回数组
let interestSet = data
.filter(obj=> obj.Interest_area && obj.Session_type === "Course information session")
.reduce((a, c)=> a.add(...c.Interest_area.split(',')), new Set);
let uniques = [...interestSet];
console.log(uniques)

<script>
let data = [
{
"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"
}
]
</script>
&#13;
答案 1 :(得分:1)
你可以这样做:
let result = [...new Set( //Use new Set to get unique values
arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ) //Use filter to filter the Session_type and Interest_area is not blank
.reduce((c,v)=>c.concat(v.Interest_area.split(',')),[])) //Use reduce and concat to flatten the array
.map(o=>o.trim()) //Use map to trim the values
]
这是一个片段:
let arr=[{"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"}];
let search = 'Course information session';
let result = [...new Set(arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ).reduce((c,v)=>c.concat(v.Interest_area.split(',')),[]).map(o=>o.trim()))]
console.log(result);
答案 2 :(得分:0)
您必须在每个子阵列中循环并检查会话类型的值,然后提取感兴趣区域
var obj = [
{
"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"
}
]
var interest_list = [];
$.each(obj, function(event,details){
if(details['Session_type'] === 'Course information session'){
var addnew = details['Interest_area'].split(",");
interest_list.push(addnew);
}
}
console.log(interest_list);