jQuery unique, difference in Chrome and Firefox?

4

I'm having an unexpected behavior with unique of jQuery.

The following command:

 var x = [1,2,1,2];
 var y = $.unique(x);
 document.write(y);

In Chrome it results in 1,2 (which is correct), but in Firefox it appears 1,2,1,2 .

Is this a bug, or was it supposed to be anyway?

Follow the example in jsfiddle , open it first in Chrome (works correctly) and then in Firefox (it does not work).

OBS : jQuery 2.1.0; Firefox 24.5.0; Google Chrome 34.0.1847.131 m.

    
asked by anonymous 14.05.2014 / 16:47

1 answer

5

The .unique() of jQuery is not designed for numbers or strings. In the jQuery documentation you can read:

  

Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.

in our language would be:

  

Sorts an array of DOM elements, removing the duplicates. Note that this only works on DOM elements and not numbers or strings.

Here's the javascript code to do what you need: link

var arrayUnique = function(a) {
    return a.reduce(function(p, c) {
        if (p.indexOf(c) < 0) p.push(c);
        return p;
    }, []);
};

Source: link

    
14.05.2014 / 17:34