Javascript 检查数组中是否存在值(AngularJS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32610282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:56:47  来源:igfitidea点击:

Check if value exists in the array (AngularJS)

javascriptarraysangularjsobjectangularjs-controller

提问by yuro

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.

目前我正在使用forEach()angular 的方法来检查对象数组的新值。但这是错误的方法,因为例如列表中有 20 个对象。当我使用现有文章创建对象时,forEach 中的 if 语句会告诉 1 次文章存在和 19 次不存在。

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 create a new article then should be an if-Statement to check if the added artNr is already exists.

articlelist包含所有20个对象。为了进行比较,我只需要artNr. 因为当用户创建新文章时,应该是一个 if-Statement 来检查添加的 artNr 是否已经存在。

$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.artNr 返回我“未定义”,因为列表变量是一个数组:

list output in console => Array ["AB001", "AB002", "AB003", "AB004"],

createItem output: => Object { artNr: "AB001", description: "New Article" ...}

在控制台中列出输出 => Array ["AB001", "AB002", "AB003", "AB004"],

创建项目输出:=> Object { artNr: "AB001", description: "New Article" ...}

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

如何将新创建的对象与列表变量中的数组进行比较?

回答by Juha Tauriainen

You could use indexOffunction.

你可以使用indexOf函数。

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

More about indexOf:

有关 indexOf 的更多信息:

回答by Muhammad Awais

You can use indexOf(). Like:

您可以使用indexOf()。喜欢:

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 -1if the item is not found.

所述的indexOf()方法检索阵列为指定项,并返回其位置。如果未找到该项目,则返回-1



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

如果要从头开始搜索,请使用lastIndexOf()方法:

    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 -1if the item is not found.

如果未找到该项目,则返回-1

回答by Taran

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