JS如何实现flat?

545 阅读1分钟
let flat = function (target = Array, depth = 1) {
  let arr = [];
  target.forEach((element) => {
    if (Array.isArray(element) && depth) {
      arr.push(...flat(element, depth - 1));
    } else {
      arr.push(element);
    }
  });
  return arr;
};