Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. const steepArr = [[1, 2, [3]], 4]
  2. const simpleArr = [1, 2, 3, 4]
  3. const easyArr = [[1], [2], [3], [4]]
  4. const stringArr = [["$1"], ["$2"], ["$3"], ["$4"]]
  5.  
  6. const flatten = (arr, result = []) => {
  7. for(let item of arr){
  8. if (Array.isArray(item)) {
  9. flatten(item, result)
  10. } else {
  11. result.push(item)
  12. }
  13. }
  14. return result
  15. }
  16.  
  17. console.log('*** Results for steepArr: ', flatten(steepArr));
  18. console.log('*** Results for simpleArr: ', flatten(simpleArr));
  19. console.log('*** Results for easyArr: ', flatten(easyArr));
  20. console.log('*** Results for stringArr: ', flatten(stringArr));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement