Brian Litzinger Brian Litzinger - 1 year ago 356
Javascript Question

Can't access object property, even though it exists. Returns undefined

This is baffling me. I don't know how else to explain this, but here is what my debug code looks like. Below it you an see the output from these two logs. The first clearly shows the full JS object with the property I'm trying to access, but the very next line code I can't access it with config.col_id_3 (see the "undefined" in the screenshot?). Can anyone explain this? I can get access to every other property except field_id_4 as well.


This is what these console.log()s print in Console

Console output

Answer Source

The output of console.log(anObject) is misleading; the state of the object displayed is only resolved when you expand the > in the console. It is not the state of the object when you console.log'd the object.

Instead, try console.log(Object.keys(config)), or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log.

You will (usually) find the keys are being added after your console.log call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download