Multi-Dimensional Array Into Single-Dimensional Array

Published by Deep Xavier in

Write a recursive function that transforms a multi-dimensional array into a single-dimensional array.

Examples

flatten([[17.2, 500, "code"], "generate"]) ➞ [17.2, 500, "code", "generate"]

flatten([[[[[2, 14, "core"]]], 2, 3, 4]]) ➞ [2, 14, "core", 2, 3, 4]

flatten([["dimension"], "vertical", [["objective"]]]) ➞ ["dimension", "vertical", "objective"]

flatten([[[[[["construct"]]]]]]) ➞ ["construct"]

Notes

  • Array contains at least one element.
  • A recursive approach to solving this challenge is gretly advised and is more convenient than the iterative approach.
  • A similar version of this challenge can be found here..
  • If you think recursion is fun, a collection of those challenges can be found here.
Watch a quick demo on how Edabit works.