Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allowing to push duplicate items into array

Basically I'm pushing containers into an array, and once one has been pushed, I don't want to allow that same one to be pushed again.

Here is my JSfiddle: http://jsfiddle.net/9Dmcg/3/

Javascript:

$(document).ready(function(){

var favorites = [];
var counter = 0;

    $('.containers').on('click', function(){

        favorites.push($(this).clone())

        $('.favorite').append(favorites);
    });


});

I need to find a way to work around that.

like image 678
James Mitchell Avatar asked Mar 12 '13 22:03

James Mitchell


People also ask

How do you not allow duplicates in array?

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.

Can array accept duplicate values?

The brute force way require you to compare each element from array to another, hence has quadratic time complexity. You can optimize performance by using HashSet data structure, which doesn't allow duplicates. So a duplicate element is the one for which add() method of HashSet return false.

Can an array contains duplicate elements?

We know that HashSet doesn't allow duplicate values in it. We can make use of this property to check for duplicates in an array. The idea is to insert all array elements into a HashSet . Now the array contains a duplicate if the array's length is not equal to the set's size.


2 Answers

Unless there's more to that click event, you can use the .one() method in place of .on to get this functionality.

$(document).ready(function(){

    var favorites = [];
    var counter = 0;

    $('.containers').one('click', function(){

        favorites.push($(this).clone())

        $('.favorite').append(favorites);
    });    

});

http://jsfiddle.net/9Dmcg/4/

Even if there were more to it, you could still use .one():

$(document).ready(function(){

    var favorites = [];
    var counter = 0;

    $('.containers').one('click', function(){

        favorites.push($(this).clone())

        $('.favorite').append(favorites);

        $(this).on("click",function(){
             alert("Favorite Added!");
        }).triggerHandler("click");

    });    

});

http://jsfiddle.net/9Dmcg/5/

like image 123
Kevin B Avatar answered Oct 10 '22 01:10

Kevin B


Try to check for element id's I would say. Something like this:

$(document).ready(function(){

var favorites = [];
var counter = 0;

    $('.containers').bind('click', function(){
        var isAdded = false;
        for (var f = 0; f < favorites.length; f++) {
            console.log(favorites[f].id + "|" + this.id);
             if (favorites[f].id === this.id)
                 isAdded = true;
        }

        if (!isAdded)
            favorites.push($(this).clone()[0])


         console.log(favorites);
        $('.favorite').append(favorites);
    });


});

And here's working example -> http://jsfiddle.net/9Dmcg/7/

mz

like image 34
mariozski Avatar answered Oct 10 '22 01:10

mariozski