Skip to content

Instantly share code, notes, and snippets.

@cristiandley
Created December 20, 2016 04:06
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 cristiandley/54d3e5ab870ecea3be305551327888a8 to your computer and use it in GitHub Desktop.
Save cristiandley/54d3e5ab870ecea3be305551327888a8 to your computer and use it in GitHub Desktop.
Flat Array of Integers
/**
* 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