In this code i intend to find all the nested object literals in a parent object.One of my nested object has an array called places as its own property.I have a recursive function called getObjNum which goes through every property and finds nested object literals inside parent object.
In my Parent object there are three nested object literals.One of them has an array as property.So container array which is supposed to store all the found nested object literals must have 3 indexes.But array places also included as fourth index to container array.I found
typeof(array)=='object' returns true;
arr instanceof Object also returns true;
How can i prevent the place array to be inserted to container array along with other object literals??
var getValue;
var parentObj = {
parentProp: 10,
childObj: {
childProp: 20,
grandChildObj: {
y: {
z:'lol',
places:['newyork','canada','dhaka']
}
}
}
}
var arr=[];
var container=[];
var count=0;
function getObjNum(obj){
var prop=Object.getOwnPropertyNames(obj);
for(i=0;i<prop.length;i++){
if(typeof(obj[prop[i]])=='object'){
container.push(obj[prop[i]]);
count++;
getObjNum(obj[prop[i]]);
}
}
}
getObjNum(parentObj);
console.log(container);
Aucun commentaire:
Enregistrer un commentaire