Created
October 15, 2015 22:56
-
-
Save simplesthing/360c32d9c28dd7728148 to your computer and use it in GitHub Desktop.
Flatten Array of mixed type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten(obj, result){ | |
if(obj.length){ | |
obj.forEach(function(o){ | |
if(o.length || o instanceof Object) { | |
flatten(o, result); | |
} else { | |
result.push(o); | |
} | |
}); | |
} else { | |
for(var key in obj) { | |
result.push(obj[key]); | |
} | |
} | |
return result; | |
} | |
console.log(flatten([1, {"val": 2}, 3,[4, [5, 6]]], [])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment