Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // returns true or false based on if obj is actually an object.
- isObject = function(obj) {
- var type = typeof obj;
- return type === 'function' || type === 'object' && !!obj;
- };
- // returns a flattened array of what is passed in
- function flatten(arr, newArr) {
- //Loop through the array
- for (let i = 0; i < arr.length; i++) {
- // If an object, then make a recursive call, othereise add to the array.
- if (isObject(arr[i])) {
- flatten(arr[i], newArr);
- } else {
- newArr.push(arr[i]);
- }
- }
- return newArr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement