To inspect an object in JavaScript you use the
for in operator.The following function inspects an object by looping on the properties and calling itself recursively.
var objectInspector = function recursiveObjectInspector(obj) {
var string = '';
for (var propName in obj) {
if(typeof(obj[propName]) !== 'object')
string += propName+" : "+obj[propName]+",\n";
else
string += '\n'+propName+' {\n'+ recursiveObjectInspector(obj[propName])+"}\n";
}
return string;
};
See the Pen object property inspector by Massimiliano De Simone (@maxdesimone) on CodePen.
No comments:
Post a Comment