Skip to content

Instantly share code, notes, and snippets.

Paul Fitzgerald Pau1fitz

Block or report user

Report or block Pau1fitz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
Pau1fitz / flatten.js
Created Sep 22, 2019
Flatten multi-dimensional array
View flatten.js
const flatten = (arr) => arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
const multiDimensionalArray = [[1,2,[3]],4];
const flattenedArray = flatten(multiDimensionalArray);
it('flattens an array of arbitrarily nested arrays of integers into a flat array of integers', () => {
assert.equal(flatten([[1,2,[3]],4]), [1, 2, 3, 4])
You can’t perform that action at this time.