Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. // Arbitrarily nested array to flattened array. Mutates.
  2. const flatten = arr => {
  3. for (let i=0; i < arr.length; Array.isArray(arr[i]) && arr.splice(i, 1, ...arr[i]) || i++){}
  4. return arr
  5. }
  6.  
  7. // testing
  8. const nested = [[1,2,[3]], 4]
  9. const flattened = [1,2,3,4]
  10.  
  11. JSON.stringify(nested) !== JSON.stringify(flattened)
  12. JSON.stringify(flatten(nested)) === JSON.stringify(flattened)
  13. JSON.stringify(flatten(flattened)) === JSON.stringify(flattened)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement