如何防止在angularjs中的数组推送重复

时间:2022-06-14 07:37:22

my code is like this :

我的代码是这样的:

var arr = [];
arr.push(item1,item2);

so arr will contain like: ["name","thing1"]

所以arr将包含如下:[“name”,“thing1”]

But i got problem when pushing element with same exact value, how do i filter same element value but still accepting update/changes. JSFIDDLE

但是在推送具有相同精确值的元素时出现问题,如何过滤相同的元素值但仍接受更新/更改。的jsfiddle

4 个解决方案

#1


40  

You can use arr.indexOf which returns -1 if it is not found, so you can add it then.

您可以使用arr.indexOf,如果找不到则返回-1,这样您就可以添加它。

e.g.

例如

if (arr.indexOf(item) == -1) {
    arr.push(item);
}

However, this does not work in old browsers...

但是,这在旧浏览器中不起作用......

JQuery has a method ($.indexOf) that works in every browser, even very old ones.

JQuery有一个方法($ .indexOf)可以在每个浏览器中运行,甚至是很旧的浏览器。

#2


7  

Just javascript is enough.

只是javascript就足够了。

If an array contains the item its index is >= 0

如果数组包含该项,则其索引> = 0

so you can do this, if index == -1 , item does not exist in the array, so you can push unique item

所以你可以这样做,如果index == -1,数组中不存在item,那么你可以推送唯一的item

if(arr.indexOf(item) == -1) {
   arr.push(item);
}

Edit: You asked for changing the number, this is how

编辑:你要求更改号码,这是怎么回事

var index = arr.indexOf(item);
if(index > -1) { //checking if item exist in array
  arr[index]++;   // you can access that element by using arr[index], 
                 // then change it as you want, I'm just incrementing in above example
}

#3


2  

As what most of the answers have pointed out, you can use the Array.prototype.indexOf() method to determine if a value exists or not. In order to check for this, check the array of strings against your ng-models value property, selects.value, within the ng-change() event callback.

正如大多数答案所指出的那样,您可以使用Array.prototype.indexOf()方法来确定值是否存在。为了检查这一点,请在ng-change()事件回调中针对ng-models值属性choose.value检查字符串数组。

DEMO

DEMO

Javascript

使用Javascript

$scope.goChange = function(name, value){
  if(!~arr.indexOf(value)) {
          arr.push(name, value);
          console.log(arr);
  }
};

HTML

HTML

<select ng-model="selects" ng-change="goChange(item.name, selects.value)" ng-options="i as i.value for i in options">
  <option value=""></option>
</select>

#4


1  

You should use Angular Filters.

您应该使用角度滤镜。

Some implementations can be found as answers to a similar question: How to make ng-repeat filter out duplicate results

可以找到一些实现作为类似问题的答案:如何使ng-repeat过滤掉重复的结果

#1


40  

You can use arr.indexOf which returns -1 if it is not found, so you can add it then.

您可以使用arr.indexOf,如果找不到则返回-1,这样您就可以添加它。

e.g.

例如

if (arr.indexOf(item) == -1) {
    arr.push(item);
}

However, this does not work in old browsers...

但是,这在旧浏览器中不起作用......

JQuery has a method ($.indexOf) that works in every browser, even very old ones.

JQuery有一个方法($ .indexOf)可以在每个浏览器中运行,甚至是很旧的浏览器。

#2


7  

Just javascript is enough.

只是javascript就足够了。

If an array contains the item its index is >= 0

如果数组包含该项,则其索引> = 0

so you can do this, if index == -1 , item does not exist in the array, so you can push unique item

所以你可以这样做,如果index == -1,数组中不存在item,那么你可以推送唯一的item

if(arr.indexOf(item) == -1) {
   arr.push(item);
}

Edit: You asked for changing the number, this is how

编辑:你要求更改号码,这是怎么回事

var index = arr.indexOf(item);
if(index > -1) { //checking if item exist in array
  arr[index]++;   // you can access that element by using arr[index], 
                 // then change it as you want, I'm just incrementing in above example
}

#3


2  

As what most of the answers have pointed out, you can use the Array.prototype.indexOf() method to determine if a value exists or not. In order to check for this, check the array of strings against your ng-models value property, selects.value, within the ng-change() event callback.

正如大多数答案所指出的那样,您可以使用Array.prototype.indexOf()方法来确定值是否存在。为了检查这一点,请在ng-change()事件回调中针对ng-models值属性choose.value检查字符串数组。

DEMO

DEMO

Javascript

使用Javascript

$scope.goChange = function(name, value){
  if(!~arr.indexOf(value)) {
          arr.push(name, value);
          console.log(arr);
  }
};

HTML

HTML

<select ng-model="selects" ng-change="goChange(item.name, selects.value)" ng-options="i as i.value for i in options">
  <option value=""></option>
</select>

#4


1  

You should use Angular Filters.

您应该使用角度滤镜。

Some implementations can be found as answers to a similar question: How to make ng-repeat filter out duplicate results

可以找到一些实现作为类似问题的答案:如何使ng-repeat过滤掉重复的结果