JavaSript数组扁平化去重

时间:2020-07-21 22:32:50   收藏:0   阅读:71
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]

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!