Check if value exists in the array (AngularJS)

This question already has an answer here:

  • How do I check if an array includes an object in JavaScript? 40 answers

  • 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

  • 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.


    你可以用这样的东西....

      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});
        };
    
    链接地址: http://www.djcxy.com/p/13038.html

    上一篇: 检查数组的值

    下一篇: 检查数组中是否存在值(AngularJS)