簡體   English   中英

循環遍歷對象數組以查找一個值是否不同

[英]Loop through array of objects to find if one value differs

我有一個對象數組,想判斷其中一個對象的值是否與其他對象不同。

$scope.myArray = [{name:'ted', age:'18', gender: 'm'},
                  {name:'bob', age:'18', gender: 'm'},
                  {name:'ben', age:'19', gender: 'm'}
];

現在我遍歷它們...

angular.forEach($scope.myArray, function(value, key) {
  console.log(value.age);
  //logs 18, 18, 19
  //how to I evaluate if one of the ages is different?
});

你可以做這樣的事情

angular.forEach($scope.myArray, function(value, key) {
    if ($scope.myArray[0].age != value.age){
        $scope.isDifferent = true;
        return false;           
    }           
 });

看看這個小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM