因此,当指数为0,我想将其打印出来:为什么不打印这个咖啡脚本?
a = [ 1, 2, 3 ]
for i of a
if i == 0
console.log a[i]
但没有输出。
i == 0
是不正确的......
因此,当指数为0,我想将其打印出来:为什么不打印这个咖啡脚本?
a = [ 1, 2, 3 ]
for i of a
if i == 0
console.log a[i]
但没有输出。
i == 0
是不正确的......
我返回索引为字符串,如果它们解析为一个整数,它会工作
a = [ 1, 2, 3 ]
for i of a
if parseInt(i) == 0
console.log a[i]
,则不应使用of
来循环一个数组,你应该使用in
。从fine manual:
理解还可以用于迭代对象中的键和值。使用
of
表示理解对象的属性而不是数组中的值。yearsOld = max: 10, ida: 9, tim: 11 ages = for child, age of yearsOld "#{child} is #{age}"
所以你要遍历性质的数组对象,而不是它的索引。
,您应该使用下列内容之一的循环:
for e, i in a
if(i == 0)
console.log(a[i])
for e, i in a
console.log(e) if(i == 0)
console.log(e) for e, i in a when i == 0
#...
或者,因为你有一个阵列和数字指标,为什么不跳过循环,并得到正确的一点是:
console.log(a[0])
是的,你是对的!我只是在几分钟之前就明白了...... LOL谢谢:) – x1a0
@ZhangXiao但是kurmiau的回答是在你的之前发布的。所以,我建议删除你自己的答案,并接受这个答案。 –
@RobW你是对的。我去做 :) – x1a0