[英]AngularJs - check if value exists in array object
var SelectedOptionId = 957;
$scope.array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]
Is there a way of checking if a value exists in an that kind of array objects. 有没有一种方法可以检查这种数组对象中是否存在值。 I am using Angular and underscore.
我正在使用Angular和下划线。
I have tried all this - 我已经尝试了所有这一切-
if ($scope.array.indexOf(SelectedOptionId) === -1) {console.log('already exists')}
and 和
console.log($scope.array.hasOwnProperty(SelectedOptionId)); //returns false
and 和
console.log(_.has($scope.array, SelectedOptionId)); //returns false
You could use Array#some
and check with in
operator. 您可以使用
Array#some
并使用in
运算符进行检查。
exists = $scope.array.some(function (o) {
return SelectedOptionId in o;
});
Check this 检查一下
function checkExists (type) {
return $scope.array.some(function (obj) {
return obj === type;
}
}
var chkval=checkExists("your value")
You can use filter for this. 您可以为此使用过滤器 。 The following code should return you output array with matching results, if it exists, otherwise it will return an empty array :
以下代码应返回具有匹配结果的输出数组(如果存在),否则它将返回一个空数组:
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);
For your input it returns: 对于您的输入,它返回:
[ { '957': '1269' }, { '957': '1269' } ]
Try this: 尝试这个:
if($scope.array[SelectedOptionId] || _.includes(_.values($scope.array, SelectedOptionId))) { }
That should cover both a key and a value. 那应该既包含键又包含值。
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);
console.log(_.some($scope.array, function(o) { return _.has(o, "957"); }));
使用下划线
You can do it using the in
operator or the hasOwnProperty
function, to check for the existence of a key in an object inside the given array. 您可以使用
in
运算符或hasOwnProperty
函数来执行此操作,以检查给定数组内的对象中是否存在键。
The way you've tried using hasOwnProperty
function didn't work because you were checking it directly on the array instead of checking against the items in the array. 您尝试使用
hasOwnProperty
函数的方法不起作用,因为您直接在数组上检查它,而不是检查数组中的项目。
Check the below code snippet. 检查以下代码段。
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>
Different ways to do this : 不同的方式来做到这一点:
Working demo : 工作演示:
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.
Working demo : 工作演示:
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."); }
Working demo : 工作演示:
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."); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.