How to flatten array in jQuery?

后端 未结 10 796
感动是毒
感动是毒 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 02:54

    You can use jQuery.map, which is the way to go if you have the jQuery Library already loaded.

    $.map( [1, 2, [3, 4], [5, 6], 7], function(n){
       return n;
    });
    

    Returns

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

提交回复
热议问题