Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var number_list = [[1, 2, [3]], 4];
- function flatten(n_list){
- // empty final list to storage the numbers
- var flattened_list = [];
- // for each list item, if item type if a number, add to the flattened_list, else,
- // iterate over each element of the array doing the same
- var func = (x) => (typeof(x) == "number") ? flattened_list.push(x) : x.map((t) => func(t)) ;
- func(n_list);
- return flattened_list;
- }
- console.log(flatten(number_list));
Add Comment
Please, Sign In to add comment