Skip to content

Instantly share code, notes, and snippets.

@Qrysto
Qrysto / flatten.js
Last active December 6, 2016 19:30
Requirement: "Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]. Your solution should be a link to a gist on gist.github.com with your implementation. When writing this code, you can use any language you're comfortable with. The code must be well te…
// Written in Javascript (ES2015)
const flatten = arg =>
typeof arg === 'number' ? [arg] :
typeof arg === 'object' && Array.isArray(arg) ?
arg.reduce(
(result, element) => result.concat(flatten(element))
, []) :
[];