Home  >  Article  >  Web Front-end  >  Detailed explanation of js array deduplication and deflattening

Detailed explanation of js array deduplication and deflattening

小云云
小云云Original
2018-03-07 11:33:421737browse

This article mainly shares with you the detailed explanation of js array deduplication and deflattening, hoping it can help everyone.

Array deduplication

var arr = [1, 43, 4, 3, 2, 4, 3];
// After removing duplicates
arr = [1, 43, 4, 3, 2]

Traditional method, for loop implementation

function dedupe(arr) {
    var rets = [];    for (var i = 0; i < arr.length; i ++) {        if (!rets.includes(arr[i])) {
            rets.push(arr[i]);
        }
    }    return rets;
}// 方法二: forEach方法实现function dedupe(arr) {
    var rets = [];
    arr && arr.forEach(function(item){
        if (!rets.includes(item)){
            rets.push(item);
        }
    });    return rets;
}

ES6 method implementation

// es6提供的新的数据结构Set,类似数组,但是成员的值都是唯一的,没有重复的值。function dedupe(arr) {
    var newSet = new Set(arr);  // arr变成了set的数据结构,并去除了其中重复的元素
    return Array.from(newSet);  // Array.from方法将set数据结构转为数组数据结构}

Array deflattening
The flattening of an array is to convert a multi-layered array array (the nesting can be any number of layers) into an array with only one layer

var arr = [1, 2, 3, [4, 3, [2, 7], 2], 5, [5, 9, 10], 7];
// After de-flattening
arr = [1, 2, 3, 4, 3, 2, 7, 2, 5, 5, 9, 10, 7];

(1) Loop recursion implementation

// for循环,如果子元素还是数组,则递归调用该方法function flatten(arr) {
    var rets = [];    for(var i = 0; i < arr.length; i ++) {        if (Array.isArray(arr[i])) {
            rets = rets.concat(flatten(arr[i]));
        } else {
            rets.push(arr[i]);
        }
    }    return rets;
}// 使用forEachfunction flatten(arr) {
    var rets = [];
    arr && arr.forEach(function(item) => {
        if (Array.isArray(item)) {
            rets = rets.concat(flatten(item));
        } else {
            rets.push(item);
        }
    });    return rets;
}

(2) Use reduce to simplify the code

function flatten(arr) {
    arr.reduce(function(pre, item){
        return pre.concat(Array.isArray(item) ? flatten(item) : item);
    }, [])
}

(3) If the array elements are all numbers, you can use the toString method

function flatten(arr) {
    var newArr = arr.toString().split(',');    return newArr.map(function(item){
        return +item;        // 将字符串转为数字
    });
}

Related recommendations:

Detailed explanation of js array deduplication and sorting

JavaScript and Python array deduplication analysis

Example detailed explanation of javascript array deduplication Several ideas

The above is the detailed content of Detailed explanation of js array deduplication and deflattening. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn