json对象: $ scope.data = [{ "姓名":" john", "年龄":" 27", "类别":{ " id":" 001", "角色":"开发者" } },{ "姓名":"大卫", "年龄":" 27", "类别":{ " id":" 002", "角色":"经理" } },{ "姓名":"戴夫", "年龄":" 27", "类别":{ " id":" 001", "角色":"开发者" } }];
输出: { "开发者":[{ "姓名":" john", "年龄":" 27", "类别":{ " id":" 001", "角色":"开发者" } },{ "姓名":"戴夫", "年龄":" 27", "类别":{ " id":" 001", "角色":"开发者" } }], "经理":[{ "姓名":"大卫", "年龄":" 27", "类别":{ " id":" 002", "角色":"经理" } }] }
答案 0 :(得分:0)
非常简单,你可以这样做:
angular.forEach($scope.data, function(value, key){
if(value.category.role === "developer")
yourObject.push(value);
});
答案 1 :(得分:0)
$scope.finalData = {};
for (var i = 0, max = $scope.data.length; i < max; i++) {
if ($scope.finalData[$scope.data[i].category.role] == undefined) {
$scope.finalData[$scope.data[i].category.role] = [];
} else {
$scope.finalData[$scope.data[i].category.role].push($scope.data[i]);
}
}
$scope.categories = Object.keys($scope.finalData);
这里我推动使用category.role作为关键字的完整对象