Edited at

Greedy flatten function for Array in JavaScript

More than 5 years have passed since last update.


via

2次元配列を1次元配列にする。 #JavaScript - Qiita


flatten with ECMAScript 5


c.f.

http://ptech.g.hatena.ne.jp/noromanba/20120622/1340396466 (written in Japanese)

http://ptech.g.hatena.ne.jp/noromanba/20120713/1342259951 (written in Japanese)


flatten.js

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]


https://gist.github.com/2975364


If you wan to use arguments.callee, as you like


see also