未捕获类型错误:Object.keys非对象调用试图访问对象 [英] Uncaught TypeError: Object.keys called on non-object while trying to access object

查看:205
本文介绍了未捕获类型错误:Object.keys非对象调用试图访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问使用 Object.keys(obj.arr))内的对象数组; 方法。如果我直接访问数组像下面,那么它给适当的输出:

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

推荐答案

如果您需要动态属性访问,您不能使用 .value的。这始终是字面上访问名为的关键。如果你想与包含在你需要使用括号变量的键名来访问属性: OBJ [值]

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]

固定演示: http://jsfiddle.net/Lv6TY/7/

console.log("Variable Pass "+Object.keys(groups[selected]));

这篇关于未捕获类型错误:Object.keys非对象调用试图访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆