简体   繁体   English

for循环中出现意外的令牌错误,为什么?

[英]Unexpected token error in for loop, why?

 var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF']; var i; for (var i = 0; i < ccode.length; i++) { var ccode[i] = fx.convert(amount, {to: 'ccode[i]'}); } 

The bit above var ccode[i] is causing an error var ccode[i]上方的位引起错误

SyntaxError: Unexpected token '['. 语法错误:意外令牌'['。 Expected ';' 预期为“;” after variable declaration. 在变量声明之后。

I am still new to JS so please bear with me. 我对JS还是陌生的,所以请忍受我。

I am editing my question here since people asked why I was re-declaring var ccode[i] and its because I need to output this: 我在这里编辑我的问题,因为人们问我为什么要重新声明var ccode [i]及其原因,因为我需要输出以下内容:

var EUR = fx.convert(amount, {to: "EUR"});
var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
var i;
  for (i = 0; i < ccode.length; i++) {
     ccode[i] = fx.convert(amount, {to: 'ccode[i]'});
  }

ccode is already declared.. no need to use var 已经声明了ccode ..无需使用var

Its not about declaring ccode twice, as variable declaration syntax is wrong 它不是关于两次声明ccode ,因为变量声明语法是错误的

var ccode[i]; should not be array, see for reference 不应为数组,请参阅以供参考

You should remove var . 您应该删除var

 var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF']; var i; for (var i = 0; i < ccode.length; i++) { ccode[i] = fx.convert(amount, {to: 'ccode[i]'}); } 

I would suggest using an array map: 我建议使用数组映射:

var ccode = ['EUR', 'BRL', 'RUB', 'KRW', 'RON', 'CHF'];
ccode = ccode.map(function(code) {
  return fx.convert(amount, {to: code})
})

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

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