我目前在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"
}
答案 0 :(得分:1)
您可以按如下方式更新阵列:
chart1data[index].Temperature = inputdata1;
小提琴:http://jsfiddle.net/KyleMuir/sPTG8/1/
希望这有帮助
答案 1 :(得分:0)
如果你想改变60,那么:
chart1data[0].Temperature = inputdata1;
否则
chart1data[index].Temperature = inputdata1;
答案 2 :(得分:0)
简而言之,您有array
object
。所述对象包含2个属性Time
和Temperature
。要访问数组中的对象,假设您知道索引,可以执行以下操作:
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'
}
}