Is there an easy way to make nested array flat?

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

That is to make this:

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

into:

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

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 10:19

    What about this one liner code ?

    console.log([['dog', 'cat', ['chicken', 'bear']], [['mouse', 'horse'], 'lion']].join().split(','));

    basically join will make comma separated string from nested array and using split you can get 1d array, nice ? bonus it'll work on all major browsers as well :)

提交回复
热议问题