Without using Array.prototype.flat()
implement a flatten
function.
Eg.
flatten([1, [2, [3]], 4]) // returns [1, 2, 3, 4]
function flatten(input) {
return input.reduce((arr, value) => {
return [
...arr,
...(Array.isArray(value) ? flatten(value) : [value])
];
}, []);
}
const chai = require("chai");
describe("flatten", function() {
it("should flatten an array", function() {
chai.assert(flatten([1, [2, [3]], 4]), [1, 2, 3, 4]);
chai.assert(flatten(['a', ['b', 2], 3, null, [[4], ['c']]]), ['a', 'b', 2, 3, null, 4, 'c']);
});
});