在Javascript中,如何转换字符串以便可以用来调用属性? [英] In Javascript, how do I convert a string so it can be used to call a property?
本文介绍了在Javascript中,如何转换字符串以便可以用来调用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有一个关联数组,数组中的键是对象的属性。我想循环遍历数组,并在每次交互中执行以下操作:
So, I have an associative array and the keys in the array are properties of an object. I want to loop through the array and in each interation do something like this:
Object.key
然而,这不起作用,导致返回undefined而不是属性的值。
This however does not work and results in returning undefined rather than the value of the property.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
你可以使用for ... in循环:
You can use a for ... in loop:
for (var key in obj) {
//key is a string containing the property name.
if (!obj.hasOwnProperty(key)) continue; //Skip properties inherited from the prototype
var value = obj[key];
}
这篇关于在Javascript中,如何转换字符串以便可以用来调用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文