简体   繁体   English

如何动态创建数组元素并将值(来自具有相同名称的变量)分配给元素

[英]How can I create array elements dynamically and assign values (from variable with the same names) to the elements

I am trying to create elements of an array dynamically and need to assign values to each array element (with the same names) and push the item to the array. 我正在尝试动态创建数组的元素,并且需要为每个数组元素(具有相同的名称)分配值,然后将项目推入数组。 I don't know how to achieve this. 我不知道该如何实现。 Please take a look at my code which I attached below. 请查看下面附上的代码。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 thanks. 谢谢。

I would like to have all elements of the array with a number value, not Lenght0, Lenght1...... The reason why I declare all the variables Length0, Length1..... and assign values to them is for testing purpose. 我想让数组的所有元素都带有数字值,而不是Lenght0,Lengght ............之所以声明所有变量Length0,Length1 .....,并为其分配值,是出于测试目的。 In my application, the values are available on the screen and the script suppose to read in the values from these fields and add them to the array, then do some calculations with these values. 在我的应用程序中,这些值在屏幕上可用,脚本假定从这些字段中读取值并将它们添加到数组中,然后使用这些值进行一些计算。

let Length0 = 188;
let Length1 = 54;
let Length2 = 54;
let Length3 = 312;
let Length4 = 54;
let Length5 = 54;
let Length6 = 976;
let Length7 = 54;
let Length8 = 54;
let Length9 = 169;
let Length10 = 54;
let Length11 = 54;
let Length12 = 130;
let Length13 = 57;
let Length14 = 54;
let Length15 = 260;
let Length16 = 54;

let numberOfAxles = 12;
let lengthString = '';
let Lengths = [];

let Lengths =[Length0,Length1,Length2,Length3,Length4,Length5,Length6,Length7,Length8,Length9,Length10,Length11];

document.write(Lengths+'<br><br>');

for (let i = 0; i < numberOfAxles; i++) {
    lengthString = 'Length' + i;
    Lengths.push('Length' + i);
    //document.write(Lengths +' <br><br>');
}

//document.write('Length String: ' + lengthString + '<br>');
document.write('<br>Second: ' + Lengths + '<br>');

First of all, you don't need all those Length N variables. 首先,您不需要所有这些Length N变量。 You can directly build up an array that contains numbers: 您可以直接构建一个包含数字的数组:

let lengths = [188, 54, 54, /*...*/];

Then to access a value at a certain location in the array, do 然后要访问数组中某个位置的值,请​​执行

lengths[1] // the second element, 54

or using a variable to index: 或使用变量进行索引:

let i = 0;
lengths[i]; // 188

Sidenotes: 旁注:

  • let is just as var , just better, so just use that instead. letvar ,只是更好,所以请改用它。

  • You can use the .length property to get the number of elements in an array, eg lengths.length , there is no need for numberOfAxles . 您可以使用.length属性获取数组中元素的数量,例如lengths.length ,无需numberOfAxles

I am not sure I get why you want to do this, but i think this is what you are looking for: 我不确定我为什么要这样做,但是我认为这是您想要的:

 let Length0 = 188; let Length1 = 54; let Length2 = 54; let Length3 = 312; let Length4 = 54; let Length5 = 54; let Length6 = 976; let Length7 = 54; let Length8 = 54; let Length9 = 169; let Length10 = 54; let Length11 = 54; let Length12 = 130; let Length13 = 57; let Length14 = 54; let Length15 = 260; let Length16 = 54; let numberOfAxles = 12; let Lengths = []; for (let i = 0; i < numberOfAxles; i++) { Lengths.push(eval('Length' + i)); } console.log(Lengths); document.write('Second: ' + Lengths.join(',')); 

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

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