JavaScript Array實(shí)例方法flat的實(shí)現(xiàn)
Array.prototype.flat()
flat() 方法用于將一個嵌套多層的數(shù)組進(jìn)行扁平,返回新數(shù)組。它不會改變原始數(shù)組。 flat 方法在處理多維數(shù)組時(shí)非常有用,它可以讓數(shù)組操作變得更加靈活和簡潔。
語法
flat() flat(depth)
參數(shù)
depth(可選):指定要扁平的深度,默認(rèn)值為 1。
返回值
一個新的數(shù)組,其中包含扁平完的數(shù)組元素。
用法
const arr = [0, 1, 2, [3, 4]] arr.flat() // [0, 1, 2, 3, 4]
描述
flat() 方法會忽略稀疏數(shù)組中的空槽。
flat() 方法是個淺拷貝方法,它不會改變原數(shù)組。
它只需要 this 值具有 length 屬性和整數(shù)鍵屬性即可。但是,如果要展開元素,則它們必須是數(shù)組。
實(shí)現(xiàn) flat 方法
從上面 flat 描述總結(jié)實(shí)現(xiàn) flat 時(shí)注意實(shí)現(xiàn)這三點(diǎn)。
- 需要一個參數(shù),默認(rèn)值為 1。
- flat 會忽略稀疏數(shù)組中的空槽。
- 展開的元素必須是數(shù)組。
Array.prototype.myFlat = function (depth = 1) { // 需要一個參數(shù),默認(rèn)值為 1
const result = []
const flatten = (arr, currentDepth) => {
for (let i = 0; i < arr.length; i++) {
if (Object.hasOwn(arr, i)) { // 忽略稀疏數(shù)組的空槽
if (Array.isArray(arr[i]) && currentDepth < depth) { // 展開的元素必須是數(shù)組
flatten(arr[i], ++currentDepth)
} else {
result.push(arr[i])
}
}
}
}
flatten(this, 0)
return result
}
測試用例
Array.prototype.myFlat = function (depth = 1) {
const result = []
const flatten = (arr, currentDepth) => {
for (let i = 0; i < arr.length; i++) {
if (Object.hasOwn(arr, i)) {
if (Array.isArray(arr[i]) && currentDepth < depth) {
flatten(arr[i], ++currentDepth)
} else {
result.push(arr[i])
}
}
}
}
flatten(this, 0)
return result
}
console.log('扁平普通數(shù)組')
const arr1 = [1, 2, [3, 4]]
console.log(arr1.myFlat())
console.log(arr1.flat())
// [1, 2, 3, 4]
const arr2 = [1, 2, [3, 4, [5, 6]]]
console.log(arr2.myFlat())
console.log(arr2.flat())
// [1, 2, 3, 4, [5, 6]]
const arr3 = [1, 2, [3, 4, [5, 6]]]
console.log(arr3.myFlat(2))
console.log(arr3.flat(2))
// [1, 2, 3, 4, 5, 6]
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]
console.log(arr4.myFlat(Infinity))
console.log(arr4.flat(Infinity))
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log('稀疏數(shù)組中使用 flat')
const arr5 = [1, 2, , 4, 5]
console.log(arr5.myFlat())
console.log(arr5.flat())
// [1, 2, 4, 5]
const arr6 = [1, , 3, ["a", , "c"]]
console.log(arr6.myFlat())
console.log(arr6.flat())
// [ 1, 3, "a", "c" ]
const arr7 = [1, , 3, ["a", , ["d", , "e"]]]
console.log(arr7.myFlat())
console.log(arr7.flat())
// [ 1, 3, "a", ["d", empty, "e"] ]
console.log(arr7.myFlat(2))
console.log(arr7.flat(2))
// [ 1, 3, "a", "d", "e"]
console.log('在非數(shù)組對象上使用 flat')
const arrayLike = {
length: 3,
0: [1, 2],
1: { length: 2, 0: 3, 1: 4 },
2: 5,
}
console.log(Array.prototype.myFlat.call(arrayLike))
console.log(Array.prototype.flat.call(arrayLike))
// [1, 2, { "0": 3, "1": 4, "length": 2 }, 5]
結(jié)語
到這里 Array 實(shí)例方法 flat 實(shí)現(xiàn)完成啦。
到此這篇關(guān)于JavaScript Array實(shí)例方法flat的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JavaScript Array flat實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript+html5實(shí)現(xiàn)繪制圓環(huán)的方法
這篇文章主要介紹了javascript+html5實(shí)現(xiàn)繪制圓環(huán)的方法,實(shí)例分析了javascript實(shí)現(xiàn)html5基于canvas繪制圓環(huán)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
JavaScript的宏任務(wù)和微任務(wù)有哪些以及怎樣執(zhí)行的詳解
這篇文章主要介紹了JavaScript的宏任務(wù)和微任務(wù)有哪些以及怎樣執(zhí)行的相關(guān)資料,宏任務(wù)(MacroTask)與微任務(wù)(MicroTask)是JavaScript異步執(zhí)行機(jī)制中的核心概念,直接影響代碼的執(zhí)行順序和性能,需要的朋友可以參考下2025-08-08
基于小程序請求接口wx.request封裝的類axios請求
這篇文章主要介紹了基于小程序請求接口wx.request封裝的類axios請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JS實(shí)現(xiàn)超簡潔網(wǎng)頁title標(biāo)題跑動閃爍提示效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)超簡潔網(wǎng)頁title標(biāo)題跑動閃爍提示效果代碼,涉及JavaScript結(jié)合定時(shí)函數(shù)動態(tài)操作頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

