— 1 minute read

From the FreeCodeCamp intermediate algorithms here:

Flatten a nested array.

You must account for varying levels of nesting.

FreeCodeCamp - Steamroller

A quick post today about flattening nested arrays down to one array.

However a quick google of 'flatten nested arrays in javascript' lead me here.

... which essentially takes care of this algorithm for us.

My solution:

function steamrollArray(arr) {
// I'm a steamroller, baby
return arr.flat(Infinity);

steamrollArray([1, [2], [3, [[4]]]]);

Interesting stack overflow answer about accessing different levels of arrays here.