Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between push and normal array assignment in javascript

I'm using flot api to draw charts. Look at this chart type HERE.
In that example they've used values like,

var sin = [], cos = [];
  for (var i = 0; i < 14; i += 0.5) {
        sin.push([i, Math.sin(i)]);
        cos.push([i, Math.cos(i)]);
  }

Now the chart was drawn "curved" lines, when i replaced the

sin.push([i, Math.sin(i)]);
cos.push([i, Math.cos(i)]);

with,

sin[i]=[i, Math.sin(i)];
cos[i]=[i, Math.cos(i)];

Then the chart gets drawn using straight lines. I've two questions now.

1) What is the difference between the two types of array assignments?
2) Is this because of difference in array assignments or the API behaviour?

any help greatly appreciated! Thanks.

Edit: Set i to increment by 1 and set different values using push method or default assignment, you'll get the same result.

like image 507
vkGunasekaran Avatar asked Dec 27 '22 17:12

vkGunasekaran


1 Answers

The i is not Integer. You can use only int in array index.

1) What is the difference between the two types of array assignments? A) None, except the push places next int for index.

2) Is this because of difference in array assignments or the API behaviour? A) Yes with your way the array is invalid, because the 1.5 value for a key is parsed as a string and api can not read the array as it expect.

Try using apis way or using different values for for statement.

like image 196
Lachezar Todorov Avatar answered Jan 17 '23 16:01

Lachezar Todorov