Skip to content

Instantly share code, notes, and snippets.

@rjoydip-zz
Last active February 24, 2017 08:26
Show Gist options
  • Save rjoydip-zz/7ad54cdec7ad6ef3a2297ee715a43eab to your computer and use it in GitHub Desktop.
Save rjoydip-zz/7ad54cdec7ad6ef3a2297ee715a43eab to your computer and use it in GitHub Desktop.
This is my own object traversal function and returning whether a object has a nested object or not (using recursion)
function traverse (obj, callback) {
Object.keys(obj).forEach(function(value){
if (typeof obj[value] == 'object') {
traverse(obj[value],callback);
callback(value,true);
}
else {
callback(value,false)
}
});
}
var myObj = {
id: 1,
name: 'joy',
address : {
street : 'abc',
lane: {
a : 1
}
}
};
traverse(myObj, function (value,status) {
console.log(value+ " : " +status);
})
console.log(myObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment