假设我有一个这样的对象
$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
关于javascript - AngularJS 为每个选择不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904057/