-1

I'm fairly new to JavaScript and I want to update elements in an array. I sort of took a stab at this code here.

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

Variables ^^

$('#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);
  }

});

But for some reason, the elements don't update properly, I get this:

[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}] 

What's happening?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Danazar
  • 35
  • 6

2 Answers2

0

try using this:EDITED

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

for more detail visit here:How can I display a JavaScript object?

Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

Fixed

$('#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);
  });
Danazar
  • 35
  • 6