Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Object.keys called on non-object while trying to access object

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

like image 973
apaleja Avatar asked Oct 22 '22 06:10

apaleja


1 Answers

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]));
like image 94
Esailija Avatar answered Oct 27 '22 10:10

Esailija