Steamroller

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.

Published by Zachary

Learning to code with free code camp - zed.code.blog. Reading and writing at alwaysbooks.co.uk.

Leave a comment

Design a site like this with WordPress.com
Get started