Skip to content

Instantly share code, notes, and snippets.

@simplesthing
Created October 15, 2015 22:56
Show Gist options
  • Save simplesthing/360c32d9c28dd7728148 to your computer and use it in GitHub Desktop.
Save simplesthing/360c32d9c28dd7728148 to your computer and use it in GitHub Desktop.
Flatten Array of mixed type
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