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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: