I am trying to access array inside object using Object.keys(obj.arr));
method. If I access array directly like below, then it is giving proper output:
alert(Object.keys(obj.arr));
but if I pass array name using parameter then it is giving error :
var selected = "arr";
alert(Object.keys(obj.arr));
error : Uncaught TypeError: Object.keys called on non-object
There is example : DEMO
If you need dynamic property access, you cannot use .value
. That is always literally accessing named key of "value"
. If you want to access property with the key name contained in the variable value
you need to use brackets: obj[value]
Fixed demo: http://jsfiddle.net/Lv6TY/7/
console.log("Variable Pass "+Object.keys(groups[selected]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With