简体   繁体   English

如何使用JavaScript中的变量动态访问对象属性?

[英]How do I access an object property dynamically using a variable in JavaScript?

I'm trying to filter data from JSON in JavaScript. 我正在尝试从JavaScript中的JSON过滤数据。

I define a variable a . 我定义一个变量a I want the property of whatever value a is equal to (not item.a ). 我想要a等于(而不是item.a )的属性。 So far I've been unable to find a way of doing it. 到目前为止,我还没有找到一种方法。

Everything else is working correctly because when I changed it to a specific entry (item.date for example) it works fine. 其他所有内容均正常运行,因为当我将其更改为特定条目(例如,item.date)时,它可以正常工作。 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对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM