JavaScript面試之如何實現(xiàn)數(shù)組拍平(扁平化)方法
1 什么叫數(shù)組拍平?
概念很簡單,意思是將一個“多維”數(shù)組降維,比如:
// 原數(shù)組是一個“三維”數(shù)組 const array = [1, 2, [3, 4, [5, 6], 7], 8, 9] // 可以降成二維 newArray1 = [1, 2, 3, 4, [5, 6], 7, 8, 9] // 也可以降成一維 newArray2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
數(shù)組拍平也稱數(shù)組扁平化、數(shù)組降維。
2 JS標(biāo)準(zhǔn)庫中的數(shù)組拍平方法
JavaScript標(biāo)準(zhǔn)庫中已經(jīng)實現(xiàn)了數(shù)組拍平方法Array.prototype.flat()
flat() 方法會按照一個可指定的深度遞歸遍歷數(shù)組,并將所有元素與遍歷到的子數(shù)組中的元素合并為一個新數(shù)組返回。
語法:var newArray = arr.flat([depth])
參數(shù):depth為可選值,表示要遍歷多維數(shù)組的深度,默認(rèn)值為1??梢岳斫鉃橄胍归_(或者說降維)的層數(shù)。
返回值:遍歷到的元素和子數(shù)組的元素組合成的新數(shù)組
舉例:
const array = [1, 2, [3, 4, [5, 6], 7], 8, 9] const newArray1 = array.flat() // 等價于array.flat(1);降1維 // newArray1: [1, 2, 3, 4, [ 5, 6 ], 7, 8, 9] const newArray2 = array.flat(2) // 降2維 // newArray2:[1, 2, 3, 4, 5, 6, 7, 8, 9]
特殊:
depth<=0時,返回的數(shù)組和原數(shù)組維數(shù)一樣(注意只是維數(shù)一樣,空位情況見第3點)
const array = [1, 2, [3, 4, [5, 6], 7], 8, 9] array.flat(-1) // [1, 2, [3, 4, [5, 6], 7], 8, 9]
depth=Infinity,返回的數(shù)組變成一維
array.flat(Infinity) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
原數(shù)組有空位,flat方法會消除空位,即使是flat(0)也會消除空位,所以第1點說的是“只是維數(shù)一樣”。并且flat方法展開到哪一層,空位就會消除到哪一層,再深層的空位不會消除
const array1 = [1, , 2, [3, ,4, [5, 6], 7], 8, 9] // 注意這個數(shù)組有兩個空位 array.flat(0) // [ 1, 2, [ 3, ,4, [ 5, 6 ], 7 ], 8, 9 ] // 第一個空位沒了,第二個空位還在
3 實現(xiàn)一個flat方法
flat方法展開一層(降1維)的步驟:遍歷數(shù)組,判斷當(dāng)前元素是否為數(shù)組,如果不是數(shù)組,直接保存;如果是數(shù)組,將其展開后保存
flat方法展開多層(降多維)無非就是在展開一層的基礎(chǔ)上,使用遞歸將數(shù)組子元素進行同樣的操作。
可以將這個方法拆分成三步:
1、如何遍歷數(shù)組
2、如何判斷元素是否為數(shù)組
3、遞歸
實現(xiàn)上述三步,將他們組合起來就可以得到不同的flat實現(xiàn)
3.1 如何遍歷一個數(shù)組
方法特別多,這里介紹3類:
1、for相關(guān)
- for 循環(huán)
- for...of
for...in是為遍歷對象屬性而構(gòu)建的,不建議與數(shù)組一起使用
const array = [1, 2, [3, 4, [5, 6], 7], 8, 9]
// for循環(huán)
for (let i = 0; i < array.length; i++) {
const element = array[i];
}
// for...of
for (const element of array) {
}
2、數(shù)組方法:能直接取到數(shù)組元素的方法
- forEach()
- reduce()
- map()
// forEach()
array.forEach(element => {
});
// reduce()
array.reduce((pre, cur) => {
const element = cur
}, [])
// map()
array.map(element => {
})
3、數(shù)組方法:返回遍歷器(Iterator)對象的方法
- keys()
- values()
- entries()
// 這三種方式僅僅是獲得遍歷器對象,還需搭配for...of來進行遍歷
// keys()
for (let i of array.keys()) {
const element = array[i]
}
// values()
for (let element of array.values() ) {
}
// entries()
for (let [i, element] of array.entries()) {
console.log(array[i])
console.log(element)
}
3.2 如何判斷元素是否為數(shù)組
設(shè)有一變量a,判斷其是否為數(shù)組。這里提供4種方法:
- Array有一個靜態(tài)方法Array.isArray()用于判斷某個變量是否是一個數(shù)組
- instanceof運算符用于檢測構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個實例對象的原型鏈上。
若a是數(shù)組,則其原型鏈上會出現(xiàn)Array.prototype - 通過對象的constructor判斷(此方法可能失效,因為constructor可以手動更改)
- 通過Object.prototype.toString()來判斷,該方法可以返回一個表示該對象的字符串
// 方法1 Array.isArray(a) // 方法2 a instanceof Array // 方法3 a.constructor === Array // 方法4 // 使用call來調(diào)用Object.prototype上的toString方法 Object.prototype.toString.call(a) === '[object Array]' // 不能這么判斷,因為這個toString已經(jīng)覆蓋了Object.prototype.toString // 只有Object.prototype.toString能正確判斷類型 a.toString()
3.3 遞歸
遞歸:對子元素進行同樣的操作
function flat() {
let res = []
遍歷數(shù)組 {
if (當(dāng)前元素是數(shù)組) {
flat(當(dāng)前元素)得到一維數(shù)組
將一維數(shù)組拼接到res中
} else {
res.push(當(dāng)前元素)
}
}
return res
}
3.4 初步實現(xiàn)flat方法
挑選遍歷方式和判斷數(shù)組的方式,搭配遞歸就可以初步實現(xiàn)flat方法,如:
function myFlat(arr) {
let res = [];
for (const item of arr) {
if (Array.isArray(item)) {
res = res.concat(myFlat(item));
// 注意concat方法返回一個新數(shù)組,不會改變原數(shù)組
} else {
res.push(item);
}
}
return res;
}
myFlat方法可以實現(xiàn)將"多維"數(shù)組拉平成一維數(shù)組,但是不能指定展開深度depth,并且也無法處理數(shù)組空位
4 優(yōu)化
4.1 指定展開深度
處理展開深度其實很簡單,我們可以增設(shè)一個遞歸終止條件,即depth<=0,代碼如下:
function myFlat(arr, depth = 1) {
// 若depth<=0,則直接返回
if (depth <= 0) {
return arr
}
let res = [];
for (const item of arr) {
if (Array.isArray(item)) {
// 每次遞歸調(diào)用,將depth-1
res = res.concat(myFlat(item, depth - 1));
} else {
res.push(item);
}
}
return res;
}
4.2 數(shù)組空位處理
事實上我們應(yīng)該盡量避免出現(xiàn)數(shù)組空位的情況
前面我們提到了遍歷數(shù)組的不同方法,它們對于數(shù)組空位的處理不盡相同
其中forEach、reduce、map遍歷時遇到空位會直接忽略;而for...of則不會忽略,它遇到空位會將其當(dāng)作undefined處理
4.2.1 for...of增加空位判斷
因此我們需要改進for...of遍歷數(shù)組的myFlat方法:
function myFlat(arr, depth = 1) {
if (depth <= 0) {
return arr;
}
let res = [];
for (const item of arr) {
if (Array.isArray(item)) {
res = res.concat(myFlat(item, depth - 1));
} else {
// 判斷數(shù)組空位
item !== undefined && res.push(item);
}
}
return res;
}
4.2.2 forEach、map方法遍歷
當(dāng)然也可以使用forEach、map方法來遍歷數(shù)組,這樣就不用手動判斷了
但是這里有一個特殊情況需要考慮,就是當(dāng)depth <= 0時,我們用filter方法來消除數(shù)組空位
// forEach
function myFlat(arr, depth = 1) {
if (depth <= 0) {
return arr.filter(item => item !== undefined);
}
let res = [];
arr.forEach((item) => {
if (Array.isArray(item)) {
res = res.concat(myFlat(item, depth - 1));
} else {
res.push(item);
}
});
return res;
}
// map
function myFlat(arr, depth = 1) {
if (depth <= 0) {
return arr.filter(item => item !== undefined);
}
let res = [];
arr.map((item) => {
if (Array.isArray(item)) {
res = res.concat(myFlat(item, depth - 1));
} else {
res.push(item);
}
});
return res;
}
4.2.3 reduce方法
其中,使用reduce方法實現(xiàn)的最為簡潔,也是面試中常考的方法之一
function myFlat(arr, depth = 1) {
return depth > 0
? arr.reduce(
(pre, cur) =>
pre.concat(Array.isArray(cur) ? myFlat(cur, depth - 1) : cur),
[]
)
: arr.filter((item) => item !== undefined);
}
5 其他
5.1 棧
理論上,遞歸方法通??梢赞D(zhuǎn)換成非遞歸方法,即使用棧
function myFlat(arr) {
let res = [];
const stack = [].concat(arr);
while (stack.length > 0) {
const item = stack.pop();
if (Array.isArray(item)) {
// 用擴展運算符展開一層
stack.push(...item);
} else {
item !== undefined && res.unshift(item);
}
}
return res;
}
但是此方法不能指定展開深度,只能徹底展開成一維數(shù)組
5.2 改進
針對棧不能指定展開深度的缺點進行改進,代碼如下:
function myFlat(arr, depth = 1) {
if (depth <= 0) {
return arr.filter((item) => item !== undefined);
}
let res;
let queue = [].concat(arr);
while (depth > 0) {
res = [];
queue.forEach((item) => {
if (Array.isArray(item)) {
// 注意用擴展運算符將數(shù)組展開前先用filter方法去掉空位
res.push(...item.filter((e) => e !== undefined));
} else {
res.push(item);
}
});
depth--;
queue = res;
}
return res;
}
總結(jié)
到此這篇關(guān)于JavaScript面試之如何實現(xiàn)數(shù)組拍平(扁平化)方法的文章就介紹到這了,更多相關(guān)JS實現(xiàn)數(shù)組扁平化方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery ajax應(yīng)用中iframe自適應(yīng)高度問題解決方法
很多管理系統(tǒng)中,都使用iframe進行信息內(nèi)容的展示方式,或者作為主菜單的鏈接展示內(nèi)容。使用iframe的問題就是自適應(yīng)高度的問題2014-04-04

