Guest User

Untitled

a guest
Dec 17th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. var number_list = [[1, 2, [3]], 4];
  2.  
  3. function flatten(n_list){
  4. // empty final list to storage the numbers
  5. var flattened_list = [];
  6. // for each list item, if item type if a number, add to the flattened_list, else,
  7. // iterate over each element of the array doing the same
  8. var func = (x) => (typeof(x) == "number") ? flattened_list.push(x) : x.map((t) => func(t)) ;
  9.  
  10. func(n_list);
  11.  
  12. return flattened_list;
  13. }
  14.  
  15. console.log(flatten(number_list));
Add Comment
Please, Sign In to add comment