How to Check If Value Is in Array With Angularjs

Check if value exists in the array (AngularJS)

You could use indexOf function.

if(list.indexOf(createItem.artNr) !== -1) {
$scope.message = 'artNr already exists!';
}

More about indexOf:

  • http://www.w3schools.com/jsref/jsref_indexof_array.asp
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

AngularJs - check if value exists in array object

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

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

AngularJS: How to check if value exists in array of objects?

try this approach:

  1. create object where keys are name property and total along with applicable are already calculated (Array.prototype.reduce)
  2. Iterate over keys of previously created object and transform it back to array (Object.keys and Array.prototype.map)

var res = {};res = Object.keys([    { "name": "Apple", "total": 16, "applicable": 21 },    { "name": "Cherry", "total": 12, "applicable": 27 },    { "name": "Plum", "total": 14, "applicable": 21 },    { "name": "Apple", "total": 16, "applicable": 21 },    { "name": "Cherry", "total": 12, "applicable": 27 },    { "name": "Plum", "total": 14, "applicable": 21 },    { "name": "Banana", "total": 14, "applicable": 21 },].reduce(function (res, item) {  if (res[item.name]) {    res[item.name].total += item.total;    res[item.name].applicable += item.applicable;  }  else {    res[item.name] = item;  }  return res; }, res)).map(function(key) {  return res[key];});console.log(res);

Check if a value exists in an Array object in JavaScript or Angular

You can use Array.prototype.some

var a = [   {id: 1, name: 'foo'},   {id: 2, name: 'bar'},   {id: 3, name: 'test'}];        
var isPresent = a.some(function(el){ return el.id === 2});console.log(isPresent);

AngularJS - How to check that a specific key has a specific value in an array

Given colors and cars arrays, you can filter colors by: