const list = [1, [2, [3, [4, [5]]]]];// 将list变成字符串let result = JSON.stringify(list);console.log(result); // [1,[2,[3,[4,[5]]]]] String// 先去除所有的括号let tmpResult = result.replace(/(\[|\])/g, '');// 再加上左右括号tmpResult = '[' + tmpResult + ']';let finalResult = JSON.parse(tmpResult); // 真正意义上实现的数组扁平化console.log(finalResult); // [ 1, 2, 3, 4, 5 ] Array
JavaScript数组扁平化的黑科技
原创泰瑞__ ©著作权
文章标签 JavaScript 文章分类 JavaScript 前端开发
 
            
        
- 
                    数组扁平化 && 对象扁平化1. 数组扁平化1.1 toString + split1.2 reduce1.3 join + split1.4 递归 + 循环1.5 扩展运算符1.6 flat()2. 对象扁平化 JavaScript js 数组扁平化 对象扁平化 数组
- 
                    数组扁平化... IT
 
 
                    













 
                    

 
                 
                    