Iterative solution for flattening n-th nested arrays in Javascript

后端 未结 8 1196
猫巷女王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条回答
  •  Happy的楠姐
    2021-02-04 13:33

    How about this?

    inp = [1, {a: 2}, [3], [[4, 5], 6], 7]
    out = inp;
    
    while(out.some(Array.isArray))
      out = [].concat.apply([], out);
    
    document.write(JSON.stringify(out));

提交回复
热议问题