admin管理员组文章数量:1313179
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.
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.
Share Improve this question edited Jul 19, 2011 at 7:37 vkGunasekaran asked Jul 19, 2011 at 7:23 vkGunasekaranvkGunasekaran 6,8147 gold badges52 silver badges60 bronze badges4 Answers
Reset to default 4The 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.
I think there's a problem with i
increment. It is clear that on every iteration i
is increased by 0.5, not 1. Look:
for (var i = 0; i < 14; i += 0.5)
So sin[i]=...
won't work properly. But sin.push(...)
will work.
Array indexes can't be fractions and are always strings, so the number 1.5 is being converted to a string '1.5' and the sequence will be '0', '0.5', '1', '1.5' and so on. But using push, all indexes will be integer strings, so will be 0, 1, 2, etc.
No doubt an integer based loop is being used to read the values from the array, so it is skipping the .5 ones (i.e. every second element) and giving jagged edges.
Generally this would work, but in this case you're incrementing i
by 0.5
which results in i
being a float
-value.
Look at the output of both variants:
// push
// cos
[[0, 1],
[0.5, 0.8775825618903728],
[1, 0.5403023058681398],
[1.5, 0.0707372016677029],
[2, -0.4161468365471424],
[2.5, -0.8011436155469337], ...]
// sin
[[0, 0],
[0.5, 0.479425538604203],
[1, 0.8414709848078965],
[1.5, 0.9974949866040544],
[2, 0.9092974268256817],
[2.5, 0.5984721441039564], ...]
// array-index
// cos
[[0, 1],
[1, 0.5403023058681398],
[2, -0.4161468365471424],
[3, -0.9899924966004454],
[4, -0.6536436208636119], ...]
// sin
[[0, 0],
[1, 0.8414709848078965],
[2, 0.9092974268256817],
[3, 0.1411200080598672],
[4, -0.7568024953079282], ...]
本文标签: jquerydifference between push and normal array assignment in javascriptStack Overflow
版权声明:本文标题:jquery - difference between push and normal array assignment in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741927130a2405355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论