2016-11-09 104 views
2
var SelectedOptionId = 957; 

$scope.array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}] 

有沒有一種方法可以檢查某個數組對象中是否存在某個值。我正在使用Angular和下劃線。AngularJs - 檢查數組對象中是否存在值

我已經嘗試了這一切 -

if ($scope.array.indexOf(SelectedOptionId) === -1) {console.log('already exists')} 

console.log($scope.array.hasOwnProperty(SelectedOptionId)); //returns false 

console.log(_.has($scope.array, SelectedOptionId)); //returns false 
+0

http://stackoverflow.com/a/31775970/4323328 – tdog

+0

注:'957'是一個關鍵,而不是看重 – Rajesh

+0

是它是一個鍵被推入陣列 –

回答

2

您可以使用Array#some並使用in運算符進行檢查。

exists = $scope.array.some(function (o) { 
    return SelectedOptionId in o; 
}); 
+2

我已經學會了更多,只是閱讀你的答案比谷歌搜索。感謝你的真棒。 :-) – Rajesh

+0

謝謝你的:) –

+0

如何做的值ie-而不是'957'我想爲'1269' –

0

入住這

function checkExists (type) { 
    return $scope.array.some(function (obj) { 
    return obj === type; 
    } 
} 

var chkval=checkExists("your value") 
0

您可以使用filter。下面的代碼應返回你的輸出數組匹配結果,如果它存在,否則它會返回一個空數組:

var array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]; 
 
var SelectedOptionId = 957; 
 

 
var result = array.filter(
 
    function(item) {return item[SelectedOptionId]} 
 
) 
 

 

 
console.log(result);

您的輸入返回:

[ { '957': '1269' }, { '957': '1269' } ] 
0

試試這個:

if($scope.array[SelectedOptionId] || _.includes(_.values($scope.array, SelectedOptionId))) { } 

這應該涵蓋關鍵和價值。

0

let selectedOptionId = "957"; 
 
let array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]; 
 

 
let filtered = array.filter(function(element){ 
 
    return Object.keys(element)[0] === selectedOptionId; 
 
    }); 
 

 
console.log(filtered);

0
console.log(_.some($scope.array, function(o) { return _.has(o, "957"); })); 

使用下劃線

0

您可以使用它的in運營商或hasOwnProperty功能,以檢查關鍵的對象內部存在做給定的數組。

您嘗試使用hasOwnProperty函數的方式不起作用,因爲您直接在數組上檢查它,而不是檢查數組中的項目。

檢查下面的代碼片段。

angular 
 
    .module('demo', []) 
 
    .controller('HomeController', DefaultController); 
 

 
function DefaultController() { 
 
    var vm = this; 
 
    vm.items = [{ 
 
    "957": "1269" 
 
    }, { 
 
    "958": "1265" 
 
    }, { 
 
    "956": "1259" 
 
    }, { 
 
    "957": "1269" 
 
    }, { 
 
    "947": "1267" 
 
    }]; 
 

 
    var key = '957'; 
 
    var isExists = keyExists(key, vm.items); 
 
    console.log('is ' + key + ' exists: ' + isExists); 
 

 
    function keyExists(key, items) { 
 
    for (var i = 0; i < items.length; i++) { 
 
     // if (key in items[i]) { 
 
     if (items[i].hasOwnProperty(key)) { 
 
     return true; 
 
     } 
 
    } 
 

 
    return false; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="demo"> 
 
    <div ng-controller="HomeController as home"> 
 
    {{home.items | json}} 
 
    </div> 
 
</div>

0

不同的方式來做到這一點:

  1. 使用對象hasOwnProperty()方法。

工作演示:

var SelectedOptionId = 957; 
 

 
var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]; 
 

 
function checkOption(key) { 
 
    for(var i in arrayObj) { 
 
    if(arrayObj[i].hasOwnProperty(key) == true) { 
 
     return key+" exists."; 
 
    } else { 
 
     return key+" Not exists."; 
 
    } 
 
    } 
 
}; 
 

 
console.log(checkOption(SelectedOptionId)); // 957 exists.

  • 使用陣列filter()方法。
  • 工作演示:

    var SelectedOptionId = 957; 
     
    
     
    var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]; 
     
    
     
    var result = arrayObj.filter(function(elem) { 
     
        return elem[SelectedOptionId] 
     
    }); 
     
    
     
    if(result == '') { 
     
    console.log(SelectedOptionId+" not exists."); 
     
    } else { 
     
    console.log(SelectedOptionId+" exists."); 
     
    }

  • 使用陣列some()方法通過Nina Scholz的建議。
  • 工作演示:

    var SelectedOptionId = 957; 
     
    
     
    var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]; 
     
    
     
    var result = arrayObj.some(function (o) { 
     
        return SelectedOptionId in o; 
     
    }); 
     
    
     
    if(result == '') { 
     
    console.log(SelectedOptionId+" not exists."); 
     
    } else { 
     
    console.log(SelectedOptionId+" exists."); 
     
    }

    相關問題