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

Tan Pham

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. 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>');
François Huppé

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(','));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I set refs and assign values to looped elements in ReactJS

分類Dev

How can I print multiple elements from an array inJavaSCript?

分類Dev

How can I remove all but the last N elements from an array?

分類Dev

How to create html elements dynamically?

分類Dev

How do I assign the same array values to 2 different variables

分類Dev

How to populate an array with same elements?

分類Dev

How to create an array of unique elements from a string/array in bash?

分類Dev

How can I create dynamic variable names without using a dictionary?

分類Dev

How can I select a non-sequential subset elements from an array using Scala and Spark?

分類Dev

How can I find the duplicated elements in a array and replace them?

分類Dev

Javascript: How can I swap elements of an array of objects (by reference, not index)?

分類Dev

How can I push the elements into array in a desired format

分類Dev

How can I repeat elements in place in an array in Python?

分類Dev

How to assign elements to a vector?

分類Dev

How do I get elements from an array without repetition?

分類Dev

Adding up values from array elements in MongoDB

分類Dev

How can I get the public elements from a Rust module?

分類Dev

How to change values and create new elements?

分類Dev

How can I pass multiple elements from a collection into a function with one or more of the elements being mutable?

分類Dev

Assign names to list elements without titled quotes

分類Dev

Assign names to the vector elements of a list in R

分類Dev

How can I get list of elements or data which are on same level with same attributes?

分類Dev

How can I sum values of an array with the same key sorted by category?

分類Dev

How to sort html elements according to array values

分類Dev

How to use variables to create elements in an array in BASH?

分類Dev

Getting variable values from variable names listed in array in Bash

分類Dev

How can I join list elements into particular other elements

分類Dev

How can I automatically verify that all web.config transforms have the same elements?

分類Dev

how can i add all elements of a struct array into another struct array?

Related 関連記事

  1. 1

    How can I set refs and assign values to looped elements in ReactJS

  2. 2

    How can I print multiple elements from an array inJavaSCript?

  3. 3

    How can I remove all but the last N elements from an array?

  4. 4

    How to create html elements dynamically?

  5. 5

    How do I assign the same array values to 2 different variables

  6. 6

    How to populate an array with same elements?

  7. 7

    How to create an array of unique elements from a string/array in bash?

  8. 8

    How can I create dynamic variable names without using a dictionary?

  9. 9

    How can I select a non-sequential subset elements from an array using Scala and Spark?

  10. 10

    How can I find the duplicated elements in a array and replace them?

  11. 11

    Javascript: How can I swap elements of an array of objects (by reference, not index)?

  12. 12

    How can I push the elements into array in a desired format

  13. 13

    How can I repeat elements in place in an array in Python?

  14. 14

    How to assign elements to a vector?

  15. 15

    How do I get elements from an array without repetition?

  16. 16

    Adding up values from array elements in MongoDB

  17. 17

    How can I get the public elements from a Rust module?

  18. 18

    How to change values and create new elements?

  19. 19

    How can I pass multiple elements from a collection into a function with one or more of the elements being mutable?

  20. 20

    Assign names to list elements without titled quotes

  21. 21

    Assign names to the vector elements of a list in R

  22. 22

    How can I get list of elements or data which are on same level with same attributes?

  23. 23

    How can I sum values of an array with the same key sorted by category?

  24. 24

    How to sort html elements according to array values

  25. 25

    How to use variables to create elements in an array in BASH?

  26. 26

    Getting variable values from variable names listed in array in Bash

  27. 27

    How can I join list elements into particular other elements

  28. 28

    How can I automatically verify that all web.config transforms have the same elements?

  29. 29

    how can i add all elements of a struct array into another struct array?

ホットタグ

アーカイブ