Last active
October 17, 2017 16:35
-
-
Save jasonray716/2a4757ac79460b634f1178a1a580d1dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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