my code is like this :
var arr = []; arr.push(item1,item2);
so arr
will contain like: ["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
You can use arr. indexOf which returns -1 if it is not found, so you can add it then.
To prevent adding duplicates to an array:Use the Array. includes() method to check if the value is not present in the array. If the value is not present, add it to the array using the push() method. The array will not contain any duplicate values.
You can use unique filter while using ng-repeat . If you use track by $index then unique won't work.
You can use arr.indexOf
which returns -1 if it is not found, so you can add it then.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With