我正在调用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'].
更多推荐
发布评论