我有一个这样的 JSON 对象数组
var dataSet1 = [{
"id": "1",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "5",
"grade": "X"
}, {
"id": "2",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "5",
"grade": "A"
}, {
"id": "3",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "4",
"grade": "X"
},
{
"id": "4",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "5",
"grade": "A"
}];
我想遍历这个数据集并创建一个新的对象数组,这些对象将具有相似“等级”属性的对象合并,并保留它们关联的“id”和“版本”值的列表,如下所示
“等级”属性等于“X”的旧对象
{
"id": "1",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "5",
"grade": "X"
}
{
"id": "3",
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"version": "4",
"grade": "X"
}
“等级”属性等于“X”的新对象
{
"ids":{"id":"1","id":"3"}
"engine": "Trident",
"browser": "Internet Explorer 4.0",
"platform": "Win 95+",
"versions":{ "version":"4", "version":"5"}
"grade": "X"
}
我很确定我必须像这样使用 $.each 和 $.grep 函数
$.each(dataSet1, function (index, value) {
tsaGrade = dataSet1[index].grade;
result = $.grep(dataSet1, function (e) {
return e.grade == tsaGrade;
})
并将 id 和版本放在新的数组中,但是我迷失了如何防止每个循环在下一次循环遍历数据集时排除已经分组的值。