Skip to content

Instantly share code, notes, and snippets.

@farynaio
Created October 8, 2018 17:37
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 farynaio/2ccfa3e8c00972bf78f568bde050c4e6 to your computer and use it in GitHub Desktop.
Save farynaio/2ccfa3e8c00972bf78f568bde050c4e6 to your computer and use it in GitHub Desktop.
Node.js flatten()
const { expect } = require("chai")
function flatten(array) {
return array.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), [])
}
describe("#flatten", () => {
it("should return flattened array", () => {
const input = [[1, 2, [3]], 4]
expect(flatten(input)).to.deep.equal([1, 2, 3, 4])
})
})
{
"name": "flatten",
"version": "1.0.0",
"description": "Node.js",
"scripts": {
"test": "mocha index.js"
},
"keywords": [
"Javascript",
"Mocha",
"Chai"
],
"author": "Adam Faryna",
"license": "MIT",
"dependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment