简体   繁体   English

无法发布全局数组变量

[英]Unable to publish global array variable

 let Array = [1, 2, 3, 5, 5] for (i = 0; i <= Array.length; i++) { let j = Math.abs(Array[i] - 1); // [0, 1, 2, 4, 4] Array[j] = Math.abs(array(j) *- 1); // [-1, -2, -3, 5, -5] } console.log(Array);

Uncaught ReferenceError: array is not defined未捕获的 ReferenceError:未定义数组

Can anyone please explain to me why my array will not console.log.任何人都可以向我解释为什么我的阵列不会 console.log。 I'm sorry if this is a really rudimentary question.如果这是一个非常初级的问题,我很抱歉。 Thank you谢谢

You cant create Array var since it is a reserved word for constructor Array您不能创建 Array var,因为它是构造函数 Array 的保留字

Because Array is a reserved Javascript word, you need to use an array (lowercase).因为 Array 是保留的 Javascript 字,所以需要使用数组(小写)。 Change Array -> array;更改数组 -> 数组;

And you need to change parentheses to square brackets Array[j] = Math.abs(array(j) *- 1);并且您需要将括号更改为方括号Array[j] = Math.abs(array(j) *- 1); to Array[j] = Math.abs(array[j] *- 1);Array[j] = Math.abs(array[j] *- 1);

Solution :解决方案

let array = [1, 2, 3, 5, 5]

for (i = 0; i <= array.length; i++) {
  let j = Math.abs(array[i] - 1); // [0, 1, 2, 4, 4]
  array[j] = Math.abs(array[j] *- 1); // [-1, -2, -3, 5, -5]
}
console.log(array);

You can't create a variable named Array since it is a reserved word for the constructor Array .您不能创建名为Array的变量,因为它是构造函数Array的保留字。

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

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