jQuery.unique on an array of strings

后端 未结 8 528
野趣味
野趣味 2020-11-29 05:16

The description of jQuery.unique() states:

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

相关标签:
8条回答
  • 2020-11-29 05:47
    var array=['a','b','c','a'];
    
        function unique(array)
        {
        var unique_arr=[];
        array.forEach(function(i,e)
        {
        if(unique_arr.indexOf(i)===-1) unique_arr.push(i);
        });
        return unique_arr;
        }
        console.log(unique(array));
    
    0 讨论(0)
  • 2020-11-29 05:48

    It might work on an array strings, etc, but it has not been designed for that use...

    Notice that the code for unique() is hiding in Sizzle as uniqueSort: github source

    While some of that extra code might seem like it would work on any array, pay close attention to sortOrder as defined here. It does a lot of extra work to put things in "document order" - hence why the documentation states that it should only be used on arrays of DOM elements.

    0 讨论(0)
提交回复
热议问题