Check if value exists in the array (AngularJS) [duplicate] Check if value exists in the array (AngularJS) [duplicate] angularjs angularjs

Check if value exists in the array (AngularJS) [duplicate]


You can use indexOf(). Like:

var Color = ["blue", "black", "brown", "gold"];var a = Color.indexOf("brown");alert(a);

The indexOf() method searches the array for the specified item, and returns its position. And return -1 if the item is not found.


If you want to search from end to start, use the lastIndexOf() method:

    var Color = ["blue", "black", "brown", "gold"];    var a = Color.lastIndexOf("brown");    alert(a);

The search will start at the specified position, or at the end if no start position is specified, and end the search at the beginning of the array.

Returns -1 if the item is not found.


U can use something like this....

  function (field,value) {         var newItemOrder= value;          // Make sure user hasnt already added this item        angular.forEach(arr, function(item) {            if (newItemOrder == item.value) {                arr.splice(arr.pop(item));            } });        submitFields.push({"field":field,"value":value});    };