嵌套數(shù)組的合并,扁平化數(shù)組

博客地址:https://ainyi.com/#/19

問題引入

請寫一個 flat 方法,實現(xiàn)扁平化嵌套數(shù)組

對于 [ [], [], [], ...] 數(shù)組里嵌套數(shù)組,有個需求:將里面的數(shù)組元素都放到外層數(shù)組,變成 [ , , , ...]

例如:let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
變成:arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

倒是有幾種方法:

// 模擬:執(zhí)行內(nèi)含 10000 子數(shù)組 + 子數(shù)組有 13 個元素的數(shù)組
let arr = [];

for (let i = 0; i < 10000; i++) {
  arr.push([Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100, Math.random()*100]);
}

// 1. toString、split、map (支持多維數(shù)組~~~寫法簡便,速度又快)
// 全部是數(shù)字類型,重新映射 map,不是字符串類型就不用 map
// 用時:0.246s
let newArr = [];
let nowTime = new Date();
newArr = arr.toString().split(',').map(item => +item);
console.log(new Date() - nowTime, 'toString、split、map');

// 全部數(shù)字類型的:arr.toString().split(',').map(Number); 
// 全部字符串類型的:arr.toString().split(','); 



// 2. reduce + concat,(數(shù)組元素較短時推薦,寫法簡便)
// 用時:5.7s
newArr = [];
nowTime = new Date();
// 默認指定第一次的prev為[]
newArr = arr.reduce((arr, cur) => arr.concat(cur), []);
console.log(new Date() - nowTime, 'reduce');


// 3. 雙重循環(huán)push,(數(shù)組元素較長時推薦,速度最快)
// 數(shù)組里面每個元素都必須是數(shù)組才行
// 諸如這樣 [[],[],[],[]] 才行,如果這樣 [1,[],2,[]] 不行,因為 for of 不能循環(huán)數(shù)字
// 用時:0.018 s
newArr = [];
nowTime = new Date();
for (let va of arr) {
  for (let vq of va) {
    newArr.push(vq);
  }
}
console.log(new Date() - nowTime, 'for');


// 4. concat
// 用時:3.4 s
newArr = [];
nowTime = new Date();
for (let va of arr) {
  newArr = newArr.concat(va);
}
console.log(new Date() - nowTime, 'concat');

// 5. es6 的深拷貝數(shù)組 (速度最慢)
// 數(shù)組里面每個元素都必須是數(shù)組才行
// 諸如這樣 [[],[],[],[]] 才行,如果這樣 [1,[],2,[]] 不行,因為 ...后接不能是數(shù)字
// 用時:34 s
newArr = [];
nowTime = new Date();
for (let va of arr) {
  newArr = [...newArr, ...va];
}
console.log(new Date() - nowTime, 'es6');

多維數(shù)組

let arr = [1, [[2], [3, [4]], 5], [11, [21, [22, 22.1, 22.3], 31], 33, 40]];
let newArr = [];

// toString、split、map (寫法簡便)
// 注意:數(shù)組元素非數(shù)字的時候不需要 map
newArr = arr.toString().split(',').map(item => +item);
console.log(newArr);

// reduce 遞歸寫法
let flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce( (a, b) => [...flattenDeep(a), ...flattenDeep(b)] , []) : [arr];
newArr = flattenDeep(arr);
console.log(newArr);

數(shù)組的深拷貝

// Array.from()
var arr1 = [1,2,3];
var arr2 = Array.from(arr1);
// 數(shù)組尾部添加
arr2.push(100);
console.log(arr1,arr2); // [1, 2, 3] [1, 2, 3, 100]

// [...arr] 使用這個也可以拼接數(shù)組,但是不推薦,效率太低
var arr1 = [1,2,3];
// 超引用拷貝數(shù)組
var arr2 = [...arr1];
// 數(shù)組尾部添加
arr2.push(1000);
console.log(arr1,arr2); // [1, 2, 3] [1, 2, 3, 1000]

function show(...args){
// 此時這個形式參數(shù)就是一個數(shù)組,我們可以直接push東西進來,如下
args.push(5);
console.log(args);
}
// 調(diào)用
show(1,2,3,4); // [1, 2, 3, 4, 5]

博客地址:https://ainyi.com/#/19

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容