JS Interview Question #3 (Polyfill для array.flat())

Читать другие статьи в Think1s

const arr = [2, 3, 4, [5, [6, 7, [8, 9]]]];
  function flatArray(arr, depth = 1) {
    return depth > 1
      ? arr.reduce((acc, curr) => {
          acc = acc.concat(
            Array.isArray(curr) ? flatArray(curr, depth - 1) :curr);
          return acc;
        }, [])
      : arr;
  }
  console.log(flatArray(arr, Infinity));
Вход в полноэкранный режим Выход из полноэкранного режима

Нажмите здесь Сплющивание объектов

Читать другие статьи в Think1s

Оцените статью
Procodings.ru
Добавить комментарий