Advertisement
Guest User

Untitled

a guest
May 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement