简体   繁体   English

JS:如何使用点语法将变量连接到API调用中?

[英]JS: how do I concatenate a variable into a API call with the dot syntax?

I am trying to pass a parameter in a function, which I will use in a Highcharts file. 我试图在函数中传递参数,该参数将在Highcharts文件中使用。

function getID (ID) {
    chart+ID+.series[0].update({
        ....do something
    )};
}

When I try to concatenate the ID that I get from the function, to the update call; 当我尝试将从函数获取的ID连接到更新调用时; I get an error saying 我说错了

unexpected token "." 意外的标记 ”。”

How do I use the ID in the update function? 如何在更新功能中使用ID? I thought that the + sign would concatenate strings, so 我以为+号可以连接字符串,所以

chart+ID+.series[0]

is the same as, with ID='1', to 与ID ='1'的相同

chart1.series[0] 

If chart1 is a global variable, you can do: 如果chart1是全局变量,则可以执行以下操作:

window['chart'+ID].series[0].update(...);

because global variables are automatically turned into properties of the window object. 因为全局变量会自动变成window对象的属性。

If it's not a global variable, you can't do this. 如果不是全局变量,则不能执行此操作。 It would be best to make chart an array, rather than having chart1 , chart2 , etc. Then you could do: 最好将chart为数组,而不要chart1 chart2chart2等。然后您可以执行以下操作:

chart[ID].series[0].update(...);

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

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