Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. // I just did this method to not use reduce, concat or any kind of special method from JS and trying to have the
  2. // cheapest complexity.
  3.  
  4. function flattenArray(array) {
  5. let i = 0;
  6. while (i != array.length) {
  7. let valueOrArray = array[i];
  8. if (! Array.isArray(valueOrArray)) {
  9. i++;
  10. } else {
  11. array.splice(i, 1, ...valueOrArray);
  12. }
  13. }
  14. return array;
  15. }
  16.  
  17. function test_flatten_array(){
  18. let arr1 = [[1,3,[99]],41];
  19. let arr2 = [1,[9,[5,[3],52,],8],2];
  20.  
  21. console.log('Flattened array 1:', flattenArray(arr1));
  22. console.log('Flattened array 2:', flattenArray(arr2));
  23. }
  24.  
  25. test_flatten_array();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement