Created
August 26, 2019 11:43
-
-
Save jomadoye/3610ccc6d6752d64e6fd56abec03e05c to your computer and use it in GitHub Desktop.
Flatten an array
This file contains 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
//Flatten array | |
const flatten = (arr) => [].concat.apply([], arr); | |
//To use | |
const arrToFlatten = [ | |
["1"], | |
["2"], | |
["3"], | |
["4"] | |
]; | |
flatten(arrToFlatten); | |
// Test | |
import assert from "assert" | |
describe('Should flatten array', function() { | |
it('should flatten array', function() { | |
assert.equal([[1], [2], [3]], [1,2,3]); | |
}); | |
it('should flatten empty array', function() { | |
assert.equal([], []); | |
}); | |
it('should flatten already flattened array', function() { | |
assert.equal([1,2,3], [1,2,3]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment