0

我目前在javascript中有以下数组

var chart1data = [
{ "Time": "1", "Temperature": 60, },
{ "Time": "2", "Temperature": 50, },
{ "Time": "3", "Temperature": 42, },
{ "Time": "4", "Temperature": 35, },
{ "Time": "5", "Temperature": 28, },
{ "Time": "6", "Temperature": 24, },
{ "Time": "7", "Temperature": 21, },
{ "Time": "8", "Temperature": 19, },
{ "Time": "9", "Temperature": 18, },
{ "Time": "10", "Temperature": 18, },
];

我的 HTML 中有一个按钮,当按下该按钮时,应将上述数组中的值“60”更改为另一个数字(例如 - 80)

该按钮链接到此功能。我怎样才能使它起作用?

 function updatechart (){

//This gets the number from a text box 
 var inputdata1 = document.getElementById("textbox1").innerHTML

//Now I need the code to put this number in replace of the value "60" 
 }
4

3 回答 3

1

您可以按如下方式更新数组:

chart1data[index].Temperature = inputdata1;

小提琴:http: //jsfiddle.net/KyleMuir/sPTG8/1/

希望这可以帮助

于 2013-10-02T08:40:03.130 回答
0

如果你想改变 60 那么:

chart1data[0].Temperature = inputdata1;

别的

chart1data[index].Temperature = inputdata1;
于 2013-10-02T08:43:41.367 回答
0

简单地说,你有一个arrayof object。所述对象包含 2 个属性TimeTemperature。要访问数组中的对象,假设您知道索引,您可以执行以下操作:

alert(chart1data[index].Time) // alerts the time of the first item
chart1data[index].Temperature = 60; // sets the temperature of the first item

或者,如果您想替换所有60 的温度值,您可以遍历您的项目并简单地更新它们,如下所示:

for (var i = 0; i < chart1data.length; i++) {
    if (chart1data[i].Temperature == '60') {
        chart1data[i].Temperature = '80'
    }
}
于 2013-10-02T08:44:23.650 回答