Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created August 11, 2016 19:48
Show Gist options
  • Save ben-bradley/f08c52bb38b66d6fc42dadf6c9e90259 to your computer and use it in GitHub Desktop.
Save ben-bradley/f08c52bb38b66d6fc42dadf6c9e90259 to your computer and use it in GitHub Desktop.
javascript array flattener
'use strict';
// es6
const flatten = (a) => (!Array.isArray(a)) ? [ a ] : a.reduce((f, i) => f.concat(flatten(i)), []);
// es5
/*
function flatten(ary) {
if (!Array.isArray(ary))
return [ ary ];
return ary.reduce(function(flat, item) {
return flat.concat(flatten(item));
}, []);
};
*/
export default flatten;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment