Is there an easy way to make nested array flat?

后端 未结 12 1576
执笔经年
执笔经年 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:28

    I know this is late but I also ran into a situation where I needed to make a multidimensional array into 1 array and I made a function as follows.

    function nested(arr) {
        var noNest = arr.toString().split(',').filter(Boolean),
            i = 0;
    
        for(i;i

    This also take the nested arrays inside the tested array and makes sure its one array as the final out put

提交回复
热议问题