【发布时间】:2016-01-25 01:46:00
【问题描述】:
这是来自https://stackoverflow.com/questions/34981100/jquery-loop-to-generate-variables的关注问题
所以我试图在我的函数中创建新创建的数组中的变量。然后我将不得不将每个新创建的变量调用到我的图表中。
var digits = [2012, 01, 5, 88, 2012, 01, 6, 90, 2012, 05, 9, 130];
size = 3;
data = []
while (digits.length > 0)
data.push(digits.splice(0, size));
var mysize = data.length;
for (i = 0; i < data.length; i++) {
x = 0
console.log("This is set " + i)
console.log('This is the data for array ' + i + '\n ' + data[i])
for (j = 0; j < 3; j++)
console.log(data[i][j])
我的第二个 console.log 语句以四个为一组打印出数组。我需要为这些数组中的每一个命名,然后我需要能够调用图表中的每个单独的数组,它位于一个单独的函数中。
现在每个数组都是从该行命名的
console.log('This is the data for array ' + i + '\n ' + data[i])
没有从这一行设置变量。
最终结果应该是 我的1 = [2012,1,5,88] my2 = [2012, 1,6, 90] my3 = [2012, 5, 9, 130] 而且由于我的数组中的数据可以扩展,我无法知道我需要制作多少个新的变量数组。这就是为什么代码应该自动生成新的数组变量名,并将原始数据分成四个一组。
然后我必须将每个新创建的变量分别传递到我的图表中。
【问题讨论】:
-
这是比上一个问题更好的开始,有一些代码。不幸的是仍然缺少关键信息....从什么中命名它们?预期的结果结构是什么?
-
不要将数组用作关联数组,您需要将它们转换为对象字面量。
-
好的,但是在对象字面量中,它如何自动生成键名?
标签: javascript jquery arrays