daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Nov 14th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var startArray = [[1,2,[3]],4];
  2. var endArray = [];
  3. //Enter the first recurssion to flatten the array
  4. flattenArray(startArray);
  5. console.log('Start Array:      ' + startArray);
  6. console.log('End Array:     ' + endArray);
  7. function flattenArray(input)
  8. {
  9.     for (var i = 0; i < input.length; i++)
  10.     {
  11.         if (typeof(input[i]) === "object")
  12.         {
  13.     //Recursively loop until its no longer an array object
  14.             flattenArray(input[i]);
  15.         }
  16.         else
  17.         {
  18.     //Once its no longer an array object, push the element into the endArray
  19.             endArray.push(input[i]);
  20.         }
  21.     }
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top