简体   繁体   English

如何使用JavaScript以编程方式放置此值?

[英]How can i put this values programatically using JavaScript?

I am getting some list of values from DataBase 我从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 . 意味着0指数会给我100.34,其中data.jobs [0] .low为我提供了100.34的价值,对不起我的英语。

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. 更新: 以下建议无关紧要,因为数据确实需要作为2d数据。 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] . 但这意味着你必须使用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] . 可以使用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];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM