/**
* 实现数组flat方法
* 可通过递归方式进行将数组拍平,实现flat, 默认depth为1
*/
function flat (array, depth = 1) {
const result = [];
for (const item of array) {
// r如果item为array, 并且深度>0,则进行递归,否则则直接放到结果集中
if (Array.isArray(item) && depth > 0) {
const childArray = flat(item, depth - 1);
for (const child of childArray) {
result.push(child)
}
} else {
result.push(item)
}
}
return result;
}
浙公网安备 33010602011771号