70

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?

zitrusire2520
  • 180
  • 2
  • 11
yuro
  • 2,189
  • 6
  • 40
  • 76
  • `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);` ` }` – Manoj De Mel Jan 18 '18 at 23:38

3 Answers3

143

You could use indexOf function.

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

More about indexOf:

Juha Tauriainen
  • 1,581
  • 1
  • 12
  • 14
  • Thanks.. I've forgotten the indexOf() method! – yuro Sep 16 '15 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Browser_compatibility). – Aryan Firouzian Jan 13 '19 at 20:32
  • I too forgot about this function, awesome simple answer right here. – cklimowski May 12 '20 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.

Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
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});
    };
Taran
  • 2,895
  • 25
  • 22
  • 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 records – curiousBoy Sep 22 '17 at 20:39