zoukankan      html  css  js  c++  java
  • 数组去重

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

    function unique(array) {
    // res用来存储结果
    var res = [];
    for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {
    for (var j = 0, resLen = res.length; j < resLen; j++ ) {
    if (array[i] === res[j]) {
    break;
    }
    }
    // 如果array[i]是唯一的,那么执行完循环,j等于resLen
    if (j === resLen) {
    res.push(array[i])
    }
    }
    return res;
    }

    console.log(unique(array)); // [1, "1"]

    在这个方法中,我们使用循环嵌套,最外层循环 array,里面循环 res,如果 array[i] 的值跟 res[j] 的值相等,就跳出循环,如果都不等于,说明元素是唯一的,这时候 j 的值就会等于 res 的长度,根据这个特点进行判断,将值添加进 res。

    indexOf

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

    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));

    unique API

    var array1 = [1, 2, '1', 2, 1];
    var array2 = [1, 1, '1', 2, 2];

    // 第一版
    function unique(array, isSorted) {
    var res = [];
    var seen = [];

    for (var i = 0, len = array.length; i < len; i++) {
    var value = array[i];
    if (isSorted) {
    if (!i || seen !== value) {
    res.push(value)
    }
    seen = value;
    }
    else if (res.indexOf(value) === -1) {
    res.push(value);
    }
    }
    return res;
    }

    console.log(unique(array1)); // [1, 2, "1"]
    console.log(unique(array2, true)); // [1, "1", 2]

    优化:

    var array3 = [1, 1, 'a', 'A', 2, 2];

    // 第二版
    // iteratee 英文释义:迭代 重复
    function unique(array, isSorted, iteratee) {
    var res = [];
    var seen = [];

    for (var i = 0, len = array.length; i < len; i++) {
    var value = array[i];
    var computed = iteratee ? iteratee(value, i, array) : value;
    if (isSorted) {
    if (!i || seen !== value) {
    res.push(value)
    }
    seen = value;
    }
    else if (iteratee) {
    if (seen.indexOf(computed) === -1) {
    seen.push(computed);
    res.push(value);
    }
    }
    else if (res.indexOf(value) === -1) {
    res.push(value);
    }
    }
    return res;
    }

    console.log(unique(array3, false, function(item){
    return typeof item == 'string' ? item.toLowerCase() : item
    })); // [1, "a", 2]

    在这一版也是最后一版的实现中,函数传递三个参数:

    array:表示要去重的数组,必填

    isSorted:表示函数传入的数组是否已排过序,如果为 true,将会采用更快的方法进行去重

    iteratee:传入一个函数,可以对每个元素进行重新的计算,然后根据处理的结果进行去重

    filter

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

    function unique(array) {
    var res = array.filter(function(item, index, array){
    return array.indexOf(item) === index;
    })
    return res;
    }

    console.log(unique(array));

    排序去重的方法:

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

    function unique(array) {
    return array.concat().sort().filter(function(item, index, array){
    return !index || item !== array[index - 1]
    })
    }

    console.log(unique(array));

    Object 键值对

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

    function unique(array) {
    var obj = {};
    return array.filter(function(item, index, array){
    return obj.hasOwnProperty(item) ? false : (obj[item] = true)
    })
    }

    console.log(unique(array)); // [1, 2]

  • 相关阅读:
    Socket的应用案例
    利用XStream实现对象XML话
    策略模式
    深入理解Java引用类型
    java 消息机制 ActiveMQ入门实例
    activity工作流表结构分析
    Spring MVC 之 Hello World
    如何发布Web项目到互联网
    ionic开发ios app
    ionic开发android app步骤
  • 原文地址:https://www.cnblogs.com/zhouyideboke/p/9871034.html
Copyright © 2011-2022 走看看