How can i put this values programatically using JavaScript?
I am getting some list of values from DataBase
var priceData = [];
data.jobs[i].low will give me the Value
for ( var i = 0; i < data.jobs.length; 开发者_如何转开发i++)
{
// Need Help Here
}
Now please tell me how can i construct the Array as
var priceData = [[0,100.34],[1,108.31],[2,109.40],[3,104.87],[4,106.00]
Means 0 index will give me 100.34 where data.jobs[0].low provides me 100.34 value , sorry fro my english .
Thanks
You have to create an other array, which contains the index of the iteration and the data
[i, data.jobs[i].low]
So it would be:
for (var i = 0; i < data.jobs.length; i++) {
priceData.push([i, data.jobs[i].low]);
}
Update: The below suggestions are not relevant, as the data is indeed needed as 2d data. However I will leave it in case it is of any interest.
But this means you have to access the values with priceData[x][1]
. More convenient (and natural) would be to put the values directly into the array (and not create an array of arrays):
for (var i = 0; i < data.jobs.length; i++) {
priceData.push(data.jobs[i].low);
}
which would give you can array like this:
[100.34, 108.31, 109.40, 104.87, 106.00]
which can be accessed with priceData[x]
.
In most cases there is no reason to store the index, as it is given implicitly by the position of the element in the array.
for ( var i = 0; i < data.jobs.length; i++)
{
priceData[i] = data.jobs[i];
}
精彩评论