Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js array deduplication and deflattening

小云云
Release: 2018-03-07 11:33:42
Original
1725 people have browsed it

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;
}
Copy after login

ES6 method implementation

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

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;
}
Copy after login

(2) Use reduce to simplify the code

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

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

function flatten(arr) {
    var newArr = arr.toString().split(&#39;,&#39;);    return newArr.map(function(item){
        return +item;        // 将字符串转为数字
    });
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!