利用递归实现数组的扁平化
程序员文章站
2022-05-15 13:31:56
// 利用递归实现数组的扁平化let ary = [1, [2, [3, [4, 5]]], 6]; let result = []; let fn = function(ary){ for(let i = 0 ; i < ary.length; i++){ let item = ary[i] if ......
// 利用递归实现数组的扁平化
let ary = [1, [2, [3, [4, 5]]], 6];
let result = [];
let fn = function(ary){
for(let i = 0 ; i < ary.length; i++){
let item = ary[i]
if(array.isarray(ary[i])){
fn(item);
} else {
result.push(item)
}
}
}
fn(ary);
console.log(result);
下一篇: 网页百度地图api,支持位置偏移