Is there an easy way to make nested array flat?

后端 未结 12 1564
执笔经年
执笔经年 2020-12-29 09:42

That is to make this:

[ [\'dog\',\'cat\', [\'chicken\', \'bear\'] ],[\'mouse\',\'horse\'] ]

into:

[\'dog\',\'cat\',\'chicken\',\'

12条回答
  •  借酒劲吻你
    2020-12-29 10:40

    var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
      return a.concat(b);
    });
    // flattened is [0, 1, 2, 3, 4, 5]
    

    It's note worthy that reduce isn't supported in IE 8 and lower.

    developer.mozilla.org reference

提交回复
热议问题