I need to flatten multidimensional arrays but my code only flattens one array and then stops. What is wrong? How do I get it to only transfer the elements with no arrays.
function flatten(arr) {
// I'm a steamroller, baby
arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
},[]);
}
flatten([[['a']], [['b']]]);
assert.deepEqual(flatten([[['a']], [['b']]]), ['a', 'b'], 'should flatten nested arrays');
should flatten nested arrays: expected [ [ 'a' ], [ 'b' ] ] to deeply equal [ 'a', 'b' ]