Is there an easy way to make nested array flat?

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

That is to make this:

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

into:

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

12条回答
  •  -上瘾入骨i
    2020-12-29 10:14

    The easiest way of flattening the Objects of any depth would be using the flat method

    var arr = [['dog','cat', ['chicken', 'bear']],[['mouse','horse'],'lion'] ]; 
    var flattened = arr.flat(Infinity);
    //output--> ["dog", "cat", "chicken", "bear", "mouse", "horse", "lion"]
    

    More aout Flat()

提交回复
热议问题