use*_*r37 2 javascript arrays loops
这是我目前的代码:
let array = [`1 (1).jgp`,`1 (2).jgp`,`1 (3).jgp`,`1 (4).jgp`,`1 (5).jgp`,`1 (6).jgp`,`1 (7).jgp`,`1 (8).jgp`,`1 (9).jgp`,`1 (10).jgp`,`1 (11).jgp`]
//rest of the code
Run Code Online (Sandbox Code Playgroud)
是否有更有效的方式存储数据?
我试过了:
for (i = 0; i < 12; i++) {
let array = [`1`+(i)+`.jgp`]
};
//rest of the code
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调用它返回时:Uncaught ReferenceError:数组未定义为:1:1
我也尝试过:
let array = [`1`+(i = 0; i < 12; i++)+`.jgp`]
//rest of the code
Run Code Online (Sandbox Code Playgroud)
但是返回了:Uncaught SyntaxError:意外的令牌;
请让我知道我做错了什么?或者我如何使这个代码更有效.谢谢,
您可以使用Array#从以下位置生成数组:
const arr = Array.from({ length: 10 }, (_, i) => `1 (${i + 1}).jgp`);
console.log(arr);Run Code Online (Sandbox Code Playgroud)
虽然一个简单的for循环也可以工作:
const array = []; // declare an empty array
for (let i = 1; i <= 10; i++) {
array.push(`1 (${i}).jgp`); // push each item to the array
};
console.log(array);Run Code Online (Sandbox Code Playgroud)