I'm currently working on JavaScript algorithms using TypeScript. Here are the functions I've created:
function steamrollArray(arr: any[]): any[] {
return arr.reduce(
(accum: any, val: any) =>
Array.isArray(val) ? accum.concat(steamrollArray(val)) : accum.concat(val)
, []);
}
However, I need the arguments to be flexible enough to accept multi-dimensional arrays, like in the examples below:
steamrollArray([[["a"]], [["b"]]]);
steamrollArray([1, [2], [3, [[4]]]]);
steamrollArray([1, [], [3, [[4]]]]);
steamrollArray([1, {}, [3, [[4]]]]);
What is the best way to define the arguments for this function?
I could potentially use Typescript types for this purpose as well, but it may not cover all scenarios. See more information here: typescript multidimensional array with different types.