Skip to content

Instantly share code, notes, and snippets.

@jomadoye
Created August 26, 2019 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jomadoye/3610ccc6d6752d64e6fd56abec03e05c to your computer and use it in GitHub Desktop.
Save jomadoye/3610ccc6d6752d64e6fd56abec03e05c to your computer and use it in GitHub Desktop.
Flatten an array
//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