SHARE
TWEET

Untitled

a guest May 16th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // returns true or false based on if obj is actually an object.
  2. isObject = function(obj) {
  3.   var type = typeof obj;
  4.   return type === 'function' || type === 'object' && !!obj;
  5. };
  6.  
  7. // returns a flattened array of what is passed in
  8. function flatten(arr, newArr) {
  9.   //Loop through the array
  10.   for (let i = 0; i < arr.length; i++) {
  11.     // If an object, then make a recursive call, othereise add to the array.
  12.     if (isObject(arr[i])) {
  13.       flatten(arr[i], newArr);
  14.     } else {
  15.       newArr.push(arr[i]);
  16.     }
  17.   }
  18.   return newArr;
  19. }
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