Files
2019-01-25 17:01:52 +08:00

17 lines
390 B
JavaScript

module.exports = function flatten(list, depth) {
depth = (typeof depth == 'number') ? depth : Infinity;
return _flatten(list, 1);
function _flatten(list, d) {
return list.reduce(function (acc, item) {
if (Array.isArray(item) && d < depth) {
return acc.concat(_flatten(item, d + 1));
}
else {
return acc.concat(item);
}
}, []);
}
};