Guest User

Untitled

a guest
Apr 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. function flattenArray(input) {
  2. function flatten(input, result) {
  3. input.forEach((item)=>{
  4. if (item instanceof Array) {
  5. result.concat(flatten(item, result))
  6. } else {
  7. result.push(item)
  8. }
  9. })
  10. return result
  11. }
  12. return flatten(input, [])
  13. }
  14.  
  15. console.log(flattenArray([[1,2,[3]],4,[[[5,6]],7,8],9]))
  16. console.log(flattenArray([1,2,3,4,5,6]))
  17. console.log(flattenArray([1,2,3,[4,5,[6,7,[8]]]]))
  18. console.log(flattenArray([[1,2,[3]],4]))
  19. console.log(flattenArray([1,2,3,[4,5,[6,7,[8]]], 'foo']))
Add Comment
Please, Sign In to add comment