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]
If you wan to use arguments.callee
, as you like
see also
- Array reduce method - MDN
- Arrayを拡張(flatten,max,min,sum,uniq,first,last) - 素人がプログラミングを勉強するブログ (written in Japanese)