当我按下列表中的itemList索引时,' a' 在for循环中,它工作正常 但是当我在if语句中推送索引时,它不起作用 我不知道咖啡脚本引擎背后隐藏着什么秘密。
a=[]
for item, index of itemList
a.push(index)
if item.isExchanged is true
a.push(index)
答案 0 :(得分:2)
一些猜测:
for item, index of itemList
因此,item
接受每个属性名称/数组索引。那么item.isExchanged
怎么可能是真的:
if item.isExchanged is true
# ^^^^
# string object
您是否只是简单地交换项目< - >指数:
for index, item of itemList
# ^^^^^^^^^^^
这将迭代您的对象/字典键值对。
另一方面,如果itemList
是数组,你应该写:
for item, index in itemList
# ^^^^^^^^^^^ ^^
请注意,item
和index
的顺序与原始问题的顺序相同。但我在那里使用了关键字in
。