Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to prevent duplicate values to be added to an array.

Tags:

Having an issue with my project when it comes to adding a duplicate value to an array on a click event.

when I push the clicked item's value to the array openedCards.push(card); the code allows for multiple item values to be added to the array thus creating a matched value with a single item.

I have tried wrapping this code like so if ($.inArray(card, openedCards) < 0)openedCards.push(card); i see that the match class is no longer being added to matching pairs, or any values for that matter.

here is the Here is the jsfiddle

like image 256
logikevcoder Avatar asked Oct 12 '17 03:10

logikevcoder


People also ask

How do you prevent duplicate values?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.


1 Answers

With vanilla javascript you can do this like the following:

if (array.indexOf(value) === -1) array.push(value);

Where array is your array of value's that you don't want duplicates of.


Or, you can use the following es6 syntax:

if (array.includes(value) === false) array.push(value);
like image 82
J Livengood Avatar answered Sep 27 '22 17:09

J Livengood