Recursive solution
Code\n\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n  if (n === 0) return arr;\n  let res = [
# Code\n```\n/**\n * @param {any[]} arr\n * @param {number} depth\n * @return {any[]}\n */\nvar flat = function (arr, n) {\n  if (n === 0) return arr;\n  let res = [];\n  for (let i = 0; i < arr.length; i++) {\n    if (Array.isArray(arr[i])) {\n      res.push(...flat(arr[i], n-1));\n    } else {\n      res.push(arr[i]);\n    }\n  }\n  return res;\n};\n```
['JavaScript']