Iterative solution for flattening n-th nested arrays in Javascript

后端 未结 8 1105
猫巷女王i
猫巷女王i 2021-02-04 12:46

Can anyone show me an iterative solution for the following problem? I solved it recursively but struggled with an iterative solution. (Facebook Technical Interv

8条回答
  •  礼貌的吻别
    2021-02-04 13:32

    Works, but not recommended:

    var flatten = function(input) {
        return eval("[" + JSON.stringify(input).
        replace(/\[/g,"").replace(/\]/g,"") + "]");
    }
    

提交回复
热议问题