Kunal arora
Kunal arora

Reputation: 177

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 

Upvotes: 3

Views: 16914

Answers (8)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27192

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

Upvotes: 0

Abdul Mateen Mohammed
Abdul Mateen Mohammed

Reputation: 1894

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>

Upvotes: 0

user7135007
user7135007

Reputation:

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' } ]

Upvotes: 0

Nikhil Mohanan
Nikhil Mohanan

Reputation: 1260

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

using underscore

Upvotes: 0

Weedoze
Weedoze

Reputation: 13943

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);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could use Array#some and check with in operator.

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

Upvotes: 3

rrd
rrd

Reputation: 5957

Try this:

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

That should cover both a key and a value.

Upvotes: 0

User
User

Reputation: 1363

Check this

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

var chkval=checkExists("your value")

Upvotes: 0

Related Questions