How to flatten array in jQuery?

后端 未结 10 784
感动是毒
感动是毒 2020-12-03 02:34

How to simply flatten array in jQuery? I have:

[1, 2, [3, 4], [5, 6], 7]

And I want:

[1, 2, 3, 4, 5, 6, 7]
10条回答
  •  情歌与酒
    2020-12-03 03:13

    Old question, I know, but...

    I found this works, and is fast:

    function flatten (arr) {
      b = Array.prototype.concat.apply([], arr);
      if (b.length != arr.length) {
        b = flatten(b);
      };
    
      return b;
    }
    

提交回复
热议问题