嵌套數組的合並,扁平化數組


嵌套數組的合並,扁平化數組

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

 

 

請寫一個 flat 方法,實現扁平化嵌套數組
- 對於 [ [], [], [], ...] 數組里嵌套數組,有個需求:將里面的數組元素都放到外層數組,變成 [ , , , ...]

 

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



- 倒是有幾種方法:
 1 // 模擬:執行內含 10000 子數組 + 子數組有 13 個元素的數組
 2 let arr = [];
 3 
 4 for (let i = 0; i < 10000; i++) {
 5 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]);
 6 }
 7 
 8 // 1. toString、split、map (支持多維數組~~~寫法簡便,速度又快)
 9 // 注意:數組元素非數字的時候需要改一下
10 // 用時:0.246s
11 let newArr = [];
12 let nowTime = new Date();
13 newArr = arr.toString().split(',').map(item => +item);
14 console.log(new Date() - nowTime, 'toString、split、map');
15 
16 
17 // 2. reduce + concat,(數組元素較短時推薦,寫法簡便)
18 // 用時:5.7s
19 newArr = [];
20 nowTime = new Date();
21 // 默認指定第一次的prev為[]
22 newArr = arr.reduce((arr, cur) => arr.concat(cur), []);
23 console.log(new Date() - nowTime, 'reduce');
24 
25 
26 // 3. 雙重循環push,(數組元素較長時推薦,速度最快)
27 // 數組里面每個元素都必須是數組才行
28 // 諸如這樣 [[],[],[],[]] 才行,如果這樣 [1,[],2,[]] 不行,因為 for of 不能循環數字
29 // 用時:0.018 s
30 newArr = [];
31 nowTime = new Date();
32 for (let va of arr) {
33 for (let vq of va) {
34 newArr.push(vq);
35 }
36 }
37 console.log(new Date() - nowTime, 'for');
38 
39 
40 // 4. concat
41 // 用時:3.4 s
42 newArr = [];
43 nowTime = new Date();
44 for (let va of arr) {
45 newArr = newArr.concat(va);
46 }
47 console.log(new Date() - nowTime, 'concat');
48 
49 // 5. es6 的深拷貝數組 (速度最慢)
50 // 數組里面每個元素都必須是數組才行
51 // 諸如這樣 [[],[],[],[]] 才行,如果這樣 [1,[],2,[]] 不行,因為 ...后接不能是數字
52 // 用時:34 s
53 newArr = [];
54 nowTime = new Date();
55 for (let va of arr) {
56 newArr = [...newArr, ...va];
57 }
58 console.log(new Date() - nowTime, 'es6');

 

 

多維數組

 1 let arr = [1, [[2], [3, [4]], 5], [11, [21, [22, 22.1, 22.3], 31], 33, 40]];
 2 let newArr = [];
 3 
 4 // toString、split、map (寫法簡便)
 5 // 注意:數組元素非數字的時候需要改一下
 6 newArr = arr.toString().split(',').map(item => +item);
 7 console.log(newArr);
 8 
 9 // reduce 寫法
10 let flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce( (a, b) => [...flattenDeep(a), ...flattenDeep(b)] , []) : [arr];
11 newArr = flattenDeep(arr);
12 console.log(newArr);

 

 

數組的深拷貝

 1 // Array.from()
 2 var arr1 = [1,2,3];
 3 var arr2 = Array.from(arr1);
 4 // 數組尾部添加
 5 arr2.push(100);
 6 console.log(arr1,arr2); // [1, 2, 3] [1, 2, 3, 100]
 7 
 8 // [...arr] 使用這個也可以拼接數組,但是不推薦,效率太低
 9 var arr1 = [1,2,3];
10 // 超引用拷貝數組
11 var arr2 = [...arr1];
12 // 數組尾部添加
13 arr2.push(1000);
14 console.log(arr1,arr2); // [1, 2, 3] [1, 2, 3, 1000]
15 
16 function show(...args){
17 // 此時這個形式參數就是一個數組,我們可以直接push東西進來,如下
18 args.push(5);
19 console.log(args);
20 }
21 // 調用
22 show(1,2,3,4); // [1, 2, 3, 4, 5]

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM