在不事先知道命名约定的情况下调用javascript数组(Calling a javascript array without knowing naming conventions in advanc

编程入门 行业动态 更新时间:2024-10-26 10:40:24
在不事先知道命名约定的情况下调用javascript数组(Calling a javascript array without knowing naming conventions in advance)

我正在调用ajax函数,它返回带有数据列标签的数据集(响应)。 其中一个列标签会根据发起呼叫的位置而更改。

通常,如果列标签是硬编码的,则以下代码访问响应变量:

for (var i = 0; i < response.d.length; i++) { data.setValue(i, 1, response.d[i].Emissions); }

但是,我需要能够使用自标签更改后传入的单独字符串变量来访问响应变量。 以下是我做这件事的微弱尝试,但它没有用。 这样做的正确语法是什么?

var columnLabel = 'Emissions'; for (var i = 0; i < response.d.length; i++) { data.setValue(i, 1, response.d[i].columnLabel); }

I am calling an ajax function that returns a dataset (response) with data column labels. One of the column labels changes depending on where the call was initiated from.

Normally the following code accesses the response variable if the column label is hard coded:

for (var i = 0; i < response.d.length; i++) { data.setValue(i, 1, response.d[i].Emissions); }

However, I need to be able to access the response variable using a separate string variable that is passed in since the label changes. Below is my feeble attempt at doing it, but it isn't working. What is the correct syntax for doing that?

var columnLabel = 'Emissions'; for (var i = 0; i < response.d.length; i++) { data.setValue(i, 1, response.d[i].columnLabel); }

最满意答案

你需要使用[] -operator:

data.setValue(i, 1, response.d[i][columnLabel]);

obj.property等同于obj['property'] 。

You need to use the []-operator:

data.setValue(i, 1, response.d[i][columnLabel]);

obj.property is equivalent to obj['property'].

更多推荐

本文发布于:2023-08-07 21:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   情况下   Calling   array   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!