Created
December 20, 2016 04:06
-
-
Save cristiandley/54d3e5ab870ecea3be305551327888a8 to your computer and use it in GitHub Desktop.
Flat Array of Integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Preset: ES2015 | |
* The reduce() method applies a function against an accumulator and | |
* each value of the array (from left-to-right) to reduce it to a single value. | |
* | |
* Compatibility Mobile: ALL | |
* Compatibility Desktop: | |
* Chrome (Yes) | Firefox 3.0(1.9) | IE 9 | Opera 10.5 | Safari 4.0 | |
*/ | |
const flat = array => array.reduce( | |
(a, b) => a.concat( Array.isArray(b) ? flat(b) : b), [] | |
); | |
flat([[1,2,[3]],4]); | |
/** | |
* COMPILED CODE | |
*/ | |
"use strict"; | |
var flat = function flat(arr) { | |
return arr.reduce(function (a, b) { | |
return a.concat(Array.isArray(b) ? flat(b) : b); | |
}, []); | |
}; | |
flat([[1, 2, [3]], 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment