@writ/utils
Version:
My tool kit
24 lines (21 loc) • 512 B
JavaScript
;
/**
* 数组扁平化 到指定的 深度
*
* @param {Array} array
* @param {Array} result
* @param {Number} depth
* @return {Array}
*/
function withDepth(array, result, depth) {
for (var i = 0; i < array.length; i++) {
var value = array[i]
if (depth > 0 && Array.isArray(value)) {
withDepth(value, result, depth - 1)
} else {
result.push(value)
}
}
return result
}
module.exports = withDepth;