美文网首页
数组去重

数组去重

作者: 小漠穷秋 | 来源:发表于2018-04-26 15:41 被阅读0次

    不用array api
    1.双重for循环
    2.hashmap方式,需要考虑a[1] == a['1']的情况
    function uniq(array){
    var temp = {}, r = [], len = array.length, val, type;
    for (var i = 0; i < len; i++) {
    val = array[i];
    type = typeof val;
    if (!temp[val]) {
    temp[val] = [type];
    r.push(val);
    } else if (temp[val].indexOf(type) < 0) {
    temp[val].push(type);
    r.push(val);
    }
    }
    return r;
    }

    利用array api
    1.排序后遍历推入[]
    2.set
    3.filter(indexof)
    4.reduce(indexof)
    5.遍历推入(indexof)

    相关文章

      网友评论

          本文标题:数组去重

          本文链接:https://www.haomeiwen.com/subject/koovmxtx.html