Skip to content

Instantly share code, notes, and snippets.

@noromanba noromanba/flatten.js
Created Jun 22, 2012

Embed
What would you like to do?
Greedy flatten function for Array in JavaScript via http://ptech.g.hatena.ne.jp/noromanba/20120622/1340396466
// flatten.js
// @author noromanba (https://www.hatena.ne.jp/noromanba/)
// @license Public Domain https://creativecommons.org/licenses/publicdomain/
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
var flatten = function (ary) {
return ary.reduce(function (p, c) {
return Array.isArray(c) ? p.concat(flatten(c)) : p.concat(c);
}, []);
};
// console.log(flatten([[[2], 0], [1, 3], [4, 6, [5, 7]]]));
// [2, 0, 1, 3, 4, 6, 5, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.