说我有这样的对象
$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
答案 0 :(得分:1)
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
答案 1 :(得分:0)
您可以将来自循环的变量存储在array
中,并检查该数组是否包含loop
中的数据。这是我想出的东西
var items = [];
angular.forEach($scope.products, function(key,value) {
if(items.indexOf(value) === -1) {
items.push(key);
console.log(value.type);
}
});