You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var arr = [1,2,[3,[4,5]]];
function flatten(arr) {
let result = [];
for(let item of arr) {
if(Array.isArray(item)) {
result = result.concat(flatten(item));
}else{
result.push(item);
}
}
return result;
}
console.log(flatten(arr));// [1,2,3,4,5]
数组扁平化
数组的扁平化,就是将多层嵌套的数组转换成只有一层的数组。
扁平化方法
1、递归
2、reduce
3、toString
4、es6扩展运算符...
5、underscore.js flatten实现
6. 直接调用es6
flat()
7. 正则和JSON方法处理
The text was updated successfully, but these errors were encountered: