久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

JS數(shù)組降維的實現(xiàn)Array.prototype.concat.apply([], arr)

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

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

二維數(shù)組降為一維數(shù)組

循環(huán)降維

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]

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

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方法的參數(shù)是一個元素,該元素會被直接插入到新數(shù)組中;如果參數(shù)是一個數(shù)組,該數(shù)組的各個元素將被插入到新數(shù)組中;將該特性應(yīng)用到代碼中:

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 的元素如果是一個數(shù)組,作為concat方法的參數(shù),數(shù)組中的每一個子元素會被獨立插入進新數(shù)組。利用concat方法,我們將雙重循環(huán)簡化為了單重循環(huán)。

apply和concat降維

MDN上對于apply方法的介紹

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

apply

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

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

Vue2.6.11版本源碼降維

let children = [1, 2, 3, [4, 5, 6], 7, 8, [9, 10]];// :any 可以去掉 這里是Vue通過Flow指定傳入的參數(shù)類型可以是任意類型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]

多維數(shù)組降為一維數(shù)組

遞歸降維

遞歸函數(shù)就是在函數(shù)體內(nèi)調(diào)用自己;

遞歸函數(shù)的使用要注意函數(shù)終止條件避免死循環(huán);

// 多維數(shù)組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]

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

標簽: JavaScript
主站蜘蛛池模板: 精品日韩一区二区 | 久久精品国产视频 | 久久亚洲视频 | 最新国产毛片 | 亚洲高清资源 | 午夜免费福利电影 | 亚洲黄色一区二区三区 | 国产高清精品一区二区三区 | 亚洲成人一区二区三区 | 免费成人av| 成人在线观 | 中文字幕久久久 | 欧美国产日韩在线 | 午夜成人免费视频 | 久久国产精品毛片 | 精品一区二区三区久久久 | 国产麻豆乱码精品一区二区三区 | 亚洲成人网一区 | 国产精品69毛片高清亚洲 | 久久精品一区视频 | 欧美三级视频 | 美女黄网 | 午夜家庭影院 | 亚洲一区二区三区视频 | 日韩免费一区二区 | 午夜国产精品视频 | 色必久久 | 欧美精品一区二区三区蜜桃视频 | 亚洲黄色一区二区 | 精品视频在线观看 | 精品久久久久久久久久久久久久久久久久久 | 精品久久久蜜桃 | 黄色av网站在线观看 | 最新日韩欧美 | 色黄网站 | 精品二区视频 | 久久久精品一区二区三区 | 亚洲精品免费视频 | 国产一区www | 成人免费在线网址 | 99国内精品久久久久久久 |