Facebook
From Commodious Sheep, 6 Days ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 23
  1.  
  2. function steamrollArray(arr) {
  3.   // I'm a steamroller, baby
  4.  
  5.   console.log(arr, '->')
  6.   let arrUnchanged = false
  7.   while (!arrUnchanged) {
  8.     let initialLength = arr.length
  9.     for (let i = 0 ; i < initialLength ; i++) {
  10.       if ( Array.isArray(arr[i]) ) arr.splice(i, 1, ...arr[i])
  11.     }
  12.     arrUnchanged = arr.every(elem => !Array.isArray(elem))
  13.   }
  14.   console.log(arr)
  15.   return arr;
  16. }
  17.  
  18. steamrollArray([1, [2], [3, [[4]]]]);
  19.