72

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?

1
  • indexOf method didn't work for me as I was using json object array retrieved from the local storage. After trying many different methods, I have decided to use $filter provider available in AngularJS. var articlelist = [ {id: 1, title: 'Title1'}, {id: 2, title: 'Title2'}, {id: 3, title: 'Title3'} ]var o = $filter('filter')(articlelist, { 'id': new_title.id }, true); ` if (o.length == 0) {` ` articlelist .push(new_article);` ` }`CommentedJan 18, 2018 at 23:38

3 Answers 3

146

You could use indexOf function.

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

More about indexOf:

3
  • Thanks.. I've forgotten the indexOf() method!
    – yuro
    CommentedSep 16, 2015 at 13:54
  • again simple and clean function that is available in almost all browsers. It is only not supported on very old versions of Firefox and IE.CommentedJan 13, 2019 at 20:32
  • I too forgot about this function, awesome simple answer right here.CommentedMay 12, 2020 at 13:36
32

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.

0
    1

    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}); }; 
    1
    • 3
      this is a terrible way to do it... Think about a small list of 100 records which includes duplicate records.. For each record, you are going to loop through the added recordsCommentedSep 22, 2017 at 20:39

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.