简体   繁体   English

意外令牌,ES6数组

[英]Unexpected token , ES6 array

I have the following code. 我有以下代码。
It is an array but it throws the error: 它是一个数组,但会引发错误:

 Uncaught SyntaxError: Unexpected token , 
var counting = {4, 2, 14}.map((x) => {
    var add = x + 1;
    return x * add;
});
console.log(counting);

You confuse [] array construction with {} object construction . 您将[]数组构造与{}对象构造混淆了。

 var counting = [4, 2, 14].map((x) => { return x * (x + 1); }); console.log(counting); 

I have removed the extra line of code that simply added one to x . 我删除了仅向x添加一个代码的额外代码行。 This makes the code easier to read and maintain. 这使代码更易于阅读和维护。

数组文字符号使用[]而不是{}

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

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