learningProcess icon indicating copy to clipboard operation
learningProcess copied to clipboard

数组去重

Open shimuash opened this issue 8 years ago • 0 comments

收集几种数组去重的方法

indeOf

var array = [1, 1, '1', 2]

function unique(array) {
    var res = []
    for (var i = 0, len = array.length; i < len; i++) {
        var current = array[i]
        if (res.indexOf(current) === -1) {
            res.push(current)
        }
    }
    return res
}

console.log(unique(array))

shimuash avatar Oct 04 '17 14:10 shimuash