国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JS數組降維的實現Array.prototype.concat.apply([], arr)

瀏覽:121日期:2024-05-11 14:58:13

把多維數組(尤其是二維數組)轉化為一維數組是業務開發中的常用邏輯,最近跟著黃軼老師學習Vue2.6.1.1版本源碼時,看到源碼對二維數組降維的代碼,所以這里來寫一篇,記錄一下,加強印象

二維數組降為一維數組

循環降維

let children = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10]];function simpleNormalizeChildren(children) { let reduce = []; for (let i = 0; i < children.length; i++) { if (Array.isArray(children[i])) { for (let j = 0; j < children[i].length; j++) { reduce.push(children[i][j]); } } else { reduce.push(children[i]); } } return reduce;}simpleNormalizeChildren(children) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

此方法思路簡單,利用雙重循環遍歷二維數組中的每個元素并放到新數組中。

concat降維

MDN上對于concat的介紹

“concat creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).”

concat

如果concat方法的參數是一個元素,該元素會被直接插入到新數組中;如果參數是一個數組,該數組的各個元素將被插入到新數組中;將該特性應用到代碼中:

let children = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10]];function simpleNormalizeChildren(children) { let reduce = []; for (let i = 0; i < children.length; i++) { reduce = reduce.concat(children[i]); } return reduce;}simpleNormalizeChildren(children) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

children 的元素如果是一個數組,作為concat方法的參數,數組中的每一個子元素會被獨立插入進新數組。利用concat方法,我們將雙重循環簡化為了單重循環。

apply和concat降維

MDN上對于apply方法的介紹

“The apply() method calls a function with a given this value and arguments provided as an array.”

apply

apply方法會調用一個函數,apply方法的第一個參數會作為被調用函數的this值,apply方法的第二個參數(一個數組,或類數組的對象)會作為被調用對象的arguments值,也就是說該數組的各個元素將會依次成為被調用函數的各個參數;將該特性應用到代碼中:

let children = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10]];function simpleNormalizeChildren(children) { return Array.prototype.concat.apply([], children);}simpleNormalizeChildren(children) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

children作為apply方法的第二個參數,本身是一個數組,數組中的每一個元素(還是數組,即二維數組的第二維)會被作為參數依次傳入到concat中,效果等同于[].concat(1, 2, 3, [4, 5, 6], 7, 8, [9, 10])。利用apply方法,我們將單重循環優化為了一行代碼

Vue2.6.11版本源碼降維

let children = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10]];// :any 可以去掉 這里是Vue通過Flow指定傳入的參數類型可以是任意類型function simpleNormalizeChildren(children: any) { for (let i = 0; i < children.length; i++) { if (Array.isArray(children[i])) { return Array.prototype.concat.apply([], children); } } return children;}simpleNormalizeChildren(children); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

多維數組降為一維數組

遞歸降維

遞歸函數就是在函數體內調用自己;

遞歸函數的使用要注意函數終止條件避免死循環;

// 多維數組let children = [1, [2,3], [4, [5, 6, [7, 8]]], [9, 10]];function simpleNormalizeChildren(children) { for (let i = 0; i < children.length; i++) { if (Array.isArray(children[i])) { children = Array.prototype.concat.apply([], children); for(let j =0; j<children.length; j++) { simpleNormalizeChildren(children) } } } return children;}simpleNormalizeChildren(children); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

到此這篇關于JS數組降維的實現Array.prototype.concat.apply([], arr)的文章就介紹到這了,更多相關JS數組降維內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JavaScript
主站蜘蛛池模板: 久久99热精品免费观看k影院 | 欧美69视频 | 成在线人永久免费播放视频 | a级片在线免费看 | 精品久久网 | 成年人视频在线免费看 | 大臿蕉香蕉大视频成人 | 亚洲精品高清国产一久久 | 99视频在线观看视频 | 久久不见久久见免费影院www日本 | 亚州视频在线 | 日韩精品视频免费在线观看 | 一国产一级淫片a免费播放口 | 男子操女子 | 久久久这里只有精品免费 | 国产成人精品天堂 | 91日本在线视频 | 毛片免费观看的视频在线 | 国内精品视频成人一区二区 | 中国一级毛片欧美一级毛片 | 成人亚洲综合 | 成人亚洲精品 | 看v片| 中文字幕在线看视频一区二区三区 | 亚洲自拍图片区 | 99视频精品全部在线播放 | 美女黄网站色一级毛片 | 国产精品午夜免费观看网站 | avtt天堂网永久资源手机版 | 免费人成在线观看网站品爱网 | 91精品久久久久含羞草 | 久操精品视频 | 亚洲自拍中文 | 国产aaaaa一级毛片 | 久青草青综合在线视频 | 色播亚洲精品网站 亚洲第一 | 国产成人综合网亚洲欧美在线 | 在线a亚洲视频播放在线观看 | 一级全免费视频播放 | 国内精品久久精品 | 巴西一级毛片 |