JavaScript array 7 ways to weight

1. Using an array of additional
function unique(array{
    if (!Array.isArray(array)) return;

    let newArray = [];
    for(let i=0, len=array.length; i<len; i++) {
        let itemAtIndex = array[i];
        if (!newArray.includes(itemAtIndex)) { // newArray.indexOf(itemAtIndex) === -1
            newArray.push(itemAtIndex);
        }
    }

    return newArray;
}
2.indexOf与lastIndexOf
function unique(array{
    if (!Array.isArray(array)) return;

    for(let i=0; i<array.length; i++) {
        let itemAtIndex = array[i];
        if (array.indexOf(itemAtIndex) !== array.lastIndexOf(itemAtIndex)) {
            array.splice(i, 1);
            i--; // array 与 array.length change
        }
    }

    return array; // 顺序可能会改变
}

Note: the use of an additional array of mating elements indexOf lastIndexOf remove non-recurring

function unique(array{
    if (!Array.isArray(array)) return;

    let newArray = [];
    for(let i=0, len=array.length; i<len; i++) {
        let itemAtIndex = array[i];
        if (array.indexOf(itemAtIndex) === array.lastIndexOf(itemAtIndex)) {
            newArray.push(itemAtIndex);
        }
    }

    return newArray;
}
3.filter
function unique(arr{
    return arr.filter(function(item, index, arr{
      // 取出元素, 该元素在数组中第一次出现的索引 === 当前索引值
      return arr.indexOf(item, 0) === index;
    });
}
4. The double-loop for
function unique(array{
    if (!Array.isArray(array)) return;

    for(var i=0; i<array.length; i++) {

        for(var j=i+1; j<array.length; j++) {
            if (array[j] === array[i]) {
                array.splice(j, 1);
                j--; // array 与 array.length change
            }
        }

    }

    return array;
}
5. using sort order, the adjacent elements are not equal
function unique(array{
    if(!Array.isArray(array)) return;

    array.sort();
    for (let i=0; i<array.length; i++) {
        if (i <= array.length-2) {
            if (array[i+1] === array[i]) {
                array.splice(i+11);
                i--; // array 与 array.length change
            }
        }
    }

    return array; // 顺序可能会改变
}
6. The unique characteristics of using the object key (This method can also mark the number of repeat elements); further, map consistent with the principles of object
function unique(array{
    if (!Array.isArray(array)) return;

    let obj = {};
    for(let i=0, len=array.length; i<len; i++) {
        let itemAtIndex = array[i];
        obj[itemAtIndex] = '';
    }

    let newArray = [];
    for(let key in obj) {
        newArray.push(Number(key));
    }

    return newArray; // 顺序可能会改变
}
7. Use ES6 set
function unique(array{
    if (!Array.isArray(array)) return;
    return Array.from(new Set(array)); // [...new Set(array)] 
}

Test function and test case

function ensureEqual(a, b, message{
    if (JSON.stringify(a) !== JSON.stringify(b)) {
        console.log(`***测试失败, ${JSON.stringify(a)} 不等于 ${JSON.stringify(b)}${message}`);
    }
};

ensureEqual(unique([1,2,3,3,4,4,5,5,6,1,9,3,25,4]), [1,2,3,4,5,6,9,25], 'test1');

These are JavaScript array of more conventional way to heavy; where the first three kinds are less likely to think; the other, as are the same type of data processing services in general, so the array data type value here is unity, not do mixed type of consideration.

Guess you like

Origin www.cnblogs.com/rencoo/p/11828329.html