Skip to content

Instantly share code, notes, and snippets.

@jasonray716
Last active October 17, 2017 16:35
Show Gist options
  • Save jasonray716/2a4757ac79460b634f1178a1a580d1dc to your computer and use it in GitHub Desktop.
Save jasonray716/2a4757ac79460b634f1178a1a580d1dc to your computer and use it in GitHub Desktop.
var test = require( "tape" );
// main function to flat nested array
const flatten = ( arr ) => {
return arr.reduce( ( flat, toFlatten ) => {
return flat.concat(
Array.isArray( toFlatten ) ? flatten( toFlatten ): toFlatten
);
}, []);
};
// test main function
test( 'Flatten', t => {
t.plan( 1 );
let nestedArr = [[1,2,[3]],4];
let expected = [1, 2, 3, 4];
let actual = flatten( nestedArr );
t.equals( actual.length, expected.length, 'flats nested array' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment