JavaSript数组扁平化去重

2021-04-12 03:27

阅读:646

标签:array   isarray   数组   log   turn   each   set   war   bsp   

function flattening(arr = []) {
        let newArr = [];
        arr.forEach(item => {
            if (Array.isArray(item)) {
                newArr.push(...flattening(item));
            } else {
                newArr.push(item);
            }
        });
        return [...new Set(newArr)];
    }
    
    let a = [1, ‘ok‘, ‘ok‘, 2, [3, 4], [[[[[5, 6], 7]], 8], 1, 2, 3, 3]];
    console.log(flattening(a));
// => [1, "ok", 2, 3, 4, 5, 6, 7, 8]

 

JavaSript数组扁平化去重

标签:array   isarray   数组   log   turn   each   set   war   bsp   

原文地址:https://www.cnblogs.com/coderDemo/p/13356722.html


评论


亲,登录后才可以留言!