如何使用JavaScript中的变量动态访问对象属性? [英] How do I access an object property dynamically using a variable in JavaScript?
本文介绍了如何使用JavaScript中的变量动态访问对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在JavaScript中过滤来自JSON的数据。
I’m trying to filter data from JSON in JavaScript.
我定义了变量 a
。我希望任何值的属性 a
等于(不是 item.a
)。到目前为止,我一直无法找到一种方法。
I define a variable a
. I want the property of whatever value a
is equal to (not item.a
). So far I’ve been unable to find a way of doing it.
其他所有工作都正常,因为当我将其更改为特定条目(例如item.date)时,它可以正常工作。我无法弄清楚正确的语法。
Everything else is working correctly because when I changed it to a specific entry (item.date for example) it works fine. I cannot figure out the correct syntax.
while(i< elements.length){
var a=elements[i].id;
if(elements[i].name == 'targetfeild'){
$(elements[i]).val($.map(result,function(item){var test = elements[i].id;return item.a;}));
}
i++;
}
推荐答案
尝试 item [a]
,也可以通过这种方式访问javascript对象。
Try item[a]
, javascript objects can also be accessed this way.
这篇关于如何使用JavaScript中的变量动态访问对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文