Javascript: Object have keys, but Object.keys returns empty
There are 3 factors that can cause the general behavior you describe. In your case, I think it's the first:
When you invoke
console.log(x)
, the Chrome console will show a summary of the objectx
at the time. However, when you expand the object, the value is obtained again. The detailed object you see may have changed since the call toconsole.log()
, and changes will appear in the detail.Properties may belong to an object's prototype (as opposed to "own" properties):
> x = {a:1} { a: 1 } > y = Object.create(x) > y.a 1 > y {} > Object.keys(y) []
Properties may be non-enumerable, and won't show up in key listings.
length
is a non-enumerable property.
Try this,
const keys =[];
const person = {
name : 'Jobelle',
age :22,
mob :8547391599
}
Object.getOwnPropertyNames(tempObj).forEach(prop => {
keys.push(prop);
});