flatten
Flattens an array up to the specified depth.
- Use recursion, decrementing
depth
by1
for each level of depth. - Use
Array.prototype.reduce()
andArray.prototype.concat()
to merge elements or arrays. - Base case, for
depth
equal to1
stops recursion. - Omit the second argument,
depth
, to flatten only to a depth of1
(single flatten).