Why after I replaced if (typeof obj[key] == 'object')
with if (typeof obj.key == 'object')
the result is not expect.
this is my code:
var animals = {
bird: {
color: 'blue',
action: {
fly: true,
nesting: true,
swim:false
},
eat:'insect'
}
};
function GetObjectKeys(obj, keys) {
for (let key in obj) {
//if (typeof obj.key == 'object')
if (typeof obj[key] == 'object')
{
keys.push(key);
GetObjectKeys(obj[key], keys);
}
else {
keys.push(key);
}
}
return keys;
}
arrKeys = [];
GetObjectKeys(animals, arrKeys);
//
- result expect:
[
'bird', 'color',
'action', 'fly',
'nesting', 'swim',
'eat'
]
- result when use if (typeof obj.key == 'object') :
[ 'bird' ]
Thank you!