How can I judge the javascript object whether have a child object -
suppose have object:
obj:{ child:{ x:12, y:50 }, key1:"value1", key2:"value2" }
if want traverse object,like:
for (var in obj) { }
how can judge whether object have child object,so traverse it.i know use hasownproperty
method, in situation, have no idea child object name .
you can use check if property contains object:
for (var in obj) { if(obj.hasownproperty(i) && obj[i] instanceof object) { // obj[i] child object } }
a note of precaution: remember functions , arrays both objects in javascript. can test individually using instanceof function
, instance of array
.
Comments
Post a Comment