function flatten(input) { return input.reduce((arr, value) => [...arr].concat(Array.isArray(value) ? flatten(value) : value) , []); }
- function flatten(input) {
return input.reduce((arr, value) => {return [...arr,...(Array.isArray(value) ? flatten(value) : [value])];}, []);- return input.reduce((arr, value) =>
- [...arr].concat(Array.isArray(value) ? flatten(value) : value)
- , []);
- }
describe("flatten", function() { it("should flatten an array", function() { Test.assertSimilar(flatten([1, [2, [3]], 4]), [1, 2, 3, 4]); Test.assertSimilar(flatten(['a', ['b', 2], 3, null, [[4], ['c']]]), ['a', 'b', 2, 3, null, 4, 'c']); }); });
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']);- Test.assertSimilar(flatten([1, [2, [3]], 4]), [1, 2, 3, 4]);
- Test.assertSimilar(flatten(['a', ['b', 2], 3, null, [[4], ['c']]]), ['a', 'b', 2, 3, null, 4, 'c']);
- });
- });