元素未正确更新

时间:2014-09-01 09:07:45

标签: javascript jquery arrays highcharts

我是JavaScript的新手,我想更新数组中的元素。我在这里尝试了这段代码。

var N = 2;
var Range = 64;
var array = [[0,100], [(Range),100]];

变量^^

$('#button2').click(function() {
  array.push([(Range/N),100]);
  array.sort();
  chart.series[0].setData(array);
  N=N+1;

  for (var i = 0; i < array.length; i++) {
    array[i] = [(((i+1)*Range)/N), 100];
    array.sort();
    chart.series[0].setData(array);
    i = i+1;
    alert(array);
  }

});

但由于某些原因,元素没有正确更新,我明白了:

[10:33:48.782] [{x:64, y:100}, {x:64, y:100}, {x:64, y:100}]

[10:33:50.053] [{x:48, y:100}, {x:48, y:100}, {x:48, y:100}, {x:64, y:100}]

[10:33:52.994] [{x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}]

[10:33:55.588] [{x:53.333333333333336, y:100}, {x:53.333333333333336, y:100}, {x:53.333333333333336, y:100}, {x:53.333333333333336, y:100}, {x:53.333333333333336, y:100}, {x:64, y:100}]

[10:33:57.721] [{x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}, {x:64, y:100}]

[10:33:59.298] [{x:56, y:100}, {x:56, y:100}, {x:56, y:100}, {x:56, y:100}, {x:56, y:100}, {x:56, y:100}, {x:56, y:100}, {x:64, y:100}] 

发生了什么事?

2 个答案:

答案 0 :(得分:0)

尝试使用此:已编辑

  var result = '';
for (var property in object) {// object is your array
  result += property + ': ' + object[property]+'; ';
}
alert(result);

有关详情,请访问:How can I display a JavaScript object?

答案 1 :(得分:0)

固定

$('#button2').click(function() {
  array.push([(Range),100]);
  array.sort();
  N=N+1;

  for (var i = 0; i < array.length; i++) {
    newrange = i*Range/N;
array[i] = [newrange, 100];
  }
chart.series[0].setData(array);
console.log(array);
  });