LoginSignup
6
6

More than 5 years have passed since last update.

Greedy flatten function for Array in JavaScript

Last updated at Posted at 2012-08-24

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

6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6