Files
lodash/flattenDepth.js
John-David Dalton 6cb3460fce Remove semicolons.
2017-02-05 22:22:04 -08:00

33 lines
814 B
JavaScript

import baseFlatten from './.internal/baseFlatten.js'
import toInteger from './toInteger.js'
/**
* Recursively flatten `array` up to `depth` times.
*
* @since 4.4.0
* @category Array
* @param {Array} array The array to flatten.
* @param {number} [depth=1] The maximum recursion depth.
* @returns {Array} Returns the new flattened array.
* @see flatMap, flatMapDeep, flatMapDepth, flattenDeep
* @example
*
* const array = [1, [2, [3, [4]], 5]]
*
* flattenDepth(array, 1)
* // => [1, 2, [3, [4]], 5]
*
* flattenDepth(array, 2)
* // => [1, 2, 3, [4], 5]
*/
function flattenDepth(array, depth) {
const length = array == null ? 0 : array.length
if (!length) {
return []
}
depth = depth === undefined ? 1 : toInteger(depth)
return baseFlatten(array, depth)
}
export default flattenDepth