Check if value exists in the array (AngularJS)

JavascriptArraysAngularjsObjectAngularjs Controller

Javascript Problem Overview


Currently, I'm using the forEach()-method of angular to check the new value with the array of objects. But that's the wrong approach because, for example, in the list are 20 objects. When I'm creating an object with an existing article, then the if-statement in forEach tells one time the article is existing and 19 times it isn't.

The following code:

var list = [];

articlelist.forEach(function (val) {
   list.push(val.artNr);
});

$log.info(list);

The articlelistcontains all 20 objects. For comparing, I only need the artNr. Because when the User creates a new article, then should be an if-Statement to check if the added artNr is already existing.

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

The problem is, that list.artNr returns me "undefined" because the list variable is an array:

> list output in console => Array ["AB001", "AB002", "AB003", "AB004"], > > createItem output: => Object { artNr: "AB001", description: "New Article" ...}

How can I compare the new created object with the array from the list variable?

Javascript Solutions


Solution 1 - Javascript

You could use indexOf function.

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

More about indexOf:

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyuroView Question on Stackoverflow
Solution 1 - JavascriptJuha TauriainenView Answer on Stackoverflow
Solution 2 - JavascriptMuhammad AwaisView Answer on Stackoverflow
Solution 3 - JavascriptTaranView Answer on Stackoverflow