简体   繁体   中英

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.

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.

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.

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.

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 :

  1. Using Object hasOwnProperty() method.

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. 

  1. using Array filter() method.

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."); } 

  1. using Array some() method as suggested by Nina Scholz .

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."); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM