frontend-challenges icon indicating copy to clipboard operation
frontend-challenges copied to clipboard

18 - flatten

Open jsartisan opened this issue 1 year ago • 0 comments

index.js

export function flatten(arr, depth = 1) {
  return arr.reduce((acc, value) => {
    if (Array.isArray(value) && depth > 0) {
      acc = acc.concat(flatten(value, depth - 1))
    } else {
      acc.push(value)
    }

    return acc;
  }, [])
}

jsartisan avatar Feb 29 '24 04:02 jsartisan