Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an item is already existing in an array before push using angularjs

I have three arrays:

  • arr1=["14","16","1"] — where I am selecting
  • arr2=["14"] — where I am comparing my selection from arr1
  • arr3=[] — where I am pushing the value.

How will I be able to check if my selection does not exist in arr2?

For example I selected 14 from arr1, since it is already existing in arr2, the button will be disabled and should not be pushed in arr3.

like image 695
user2756589 Avatar asked Nov 20 '13 08:11

user2756589


People also ask

How do you check if an object already exists in an array angular?

You can use indexOf().

How do you check if an item already exists in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a value exists in an array JS?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.

How do you check if an array contains a value in AngularJS?

isArray() Function in AngularJS is used to return TRUE if the reference is an array and FALSE if it is not an array. Syntax: angular. isArray(value);


1 Answers

That is a JavaScript relating question, not AngularJS. But may that answer your question:

if(arr2.indexOf("14") == -1){
  arr3.push("14");
}
like image 134
tschiela Avatar answered Sep 26 '22 20:09

tschiela