How to get unique values in an array

后端 未结 20 2372
情歌与酒
情歌与酒 2020-11-22 14:02

How can I get a list of unique values in an array? Do I always have to use a second array or is there something similar to java\'s hashmap in JavaScript?

I am going

20条回答
  •  时光说笑
    2020-11-22 14:32

    Using jQuery, here's an Array unique function I made:

    Array.prototype.unique = function () {
        var arr = this;
        return $.grep(arr, function (v, i) {
            return $.inArray(v, arr) === i;
        });
    }
    
    console.log([1,2,3,1,2,3].unique()); // [1,2,3]
    

提交回复
热议问题