我有一些嵌套对象的数据需要使用underscoreJS groupBy嵌套对象。
[
{
"_id": "58e28acba878a54951c84e36",
"InId": "5809c226e41a193f4ffc1fa5",
"StuID": "58185aacd7e86244252eb3f0",
"ExamList": {
"Type": "FinalMk",
"SchdlId": "58e28acba878a54951c84dd0",
"Exams": {
"SubjId": "5825803ec4a0aff62070d9f5",
"MaxMark": 100,
"PassMark": 50,
"ExDate": "2016-08-11T18:30:00.000Z",
"Mark": 62
}
}
},
{
"_id": "58e28acba878a54951c84e36",
"InId": "5809c226e41a193f4ffc1fa5",
"StuID": "58185aacd7e86244252eb3f0",
"ExamList": {
"Type": "FinalMk",
"SchdlId": "58e28acba878a54951c84dd0",
"Exams": {
"SubjId": "5825803ec4a0aff62070d9f6",
"MaxMark": 100,
"PassMark": 50,
"ExDate": "2016-08-09T18:30:00.000Z",
"Mark": 76
}
}
},
],
[
{
"_id": "58e28acba878a54951c84e4d",
"InId": "5809c226e41a193f4ffc1fa5",
"StuID": "58185aacd7e86244252eb3ec",
"ExamList": {
"Type": "FinalMk",
"SchdlId": "58e28acba878a54951c84dd0",
"Exams": {
"SubjId": "5825803ec4a0aff62070d9f5",
"MaxMark": 100,
"PassMark": 50,
"ExDate": "2016-08-11T18:30:00.000Z",
"Mark": 59
}
}
},
{
"_id": "58e28acba878a54951c84e4d",
"InId": "5809c226e41a193f4ffc1fa5",
"StuID": "58185aacd7e86244252eb3ec",
"ExamList": {
"Type": "FinalMk",
"SchdlId": "58e28acba878a54951c84dd0",
"Exams": {
"SubjId": "5825803ec4a0aff62070d9f6",
"MaxMark": 100,
"PassMark": 50,
"ExDate": "2016-08-09T18:30:00.000Z",
"Mark": 56
}
}
},
],
这里我必须使用“SubjId”
对嵌套对象进行分组我尝试过这些代码,但我们的输出是未定义的“考试”。
var groupBySubj = _.groupBy(docs.ExamList.Exams, 'SubjId');
答案 0 :(得分:1)
要按嵌套属性分组,可以使用以下函数:
_.groupBy(data, function(student) { return student.ExamList.Exams.SubjId; })