说我有一个这样的物体

$scope.products = {
 blueDuck: {type:'duck', price: 5},
 redDuck: {type:'duck', price: 8},
 greenWolverine: {type:'wolverine', price:15}
}


我想按类型遍历元素:

angular.forEach($scope.products, function(key, value) {
   console.log(value.type);
});


这将输出:

duck
duck
wolverine


当我想得到一个像这样的变量时:

duck
wolverine

最佳答案

 var items = []; //The temporary array to keep the values
 angular.forEach($scope.products, function(key,value) {
     if(items.indexOf(value) === -1) { //Check if the values is not already in the temp array
         items.push(key); //Push it to array
     }
 });
console.log(items); //Show the content of the array

10-02 20:09