javascript推送功能中的动态变量 [英] Dynamic variable in javascript push function
本文介绍了javascript推送功能中的动态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在javascript中使用了push函数。
I'm using push function in javascript.
var chartData = [];
for(var i=0; i<3; i++) {
chartData.push({
date: new Date(year_s,mon_s,date_s,hr_s,min_s,sec_s),
visits: chartData1[selection[i]][j].value,
customBullet: show_annotations,
balloonTextField: "testtesttest"
});
}
我需要传递一个动态变量,例如visits_1,visits_2,等等,。在上面的代码中代替访问变量。我试过访问+_+ i。但它没有用。请帮我实现这个目标。提前致谢。
I need to pass a dynamic variable like visits_1, visits_2, etc,. in place of visits variable in the above code. I have tried visits+"_"+i. But it is not working. Please help me to achieve this. Thanks in advance.
推荐答案
你必须分两步完成:
var chartData = [];
for(var i=0; i<3; i++) {
// 1. create the object
var d = {
date: new Date(year_s,mon_s,date_s,hr_s,min_s,sec_s),
customBullet: show_annotations,
balloonTextField: "testtesttest"
};
// 2. then assign the visits_i property
d['visits_'+i] = chartData1[selection[i]][j].value;
chartData.push(d);
}
这篇关于javascript推送功能中的动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文