代码
list = {}
list['blubber'] = 'it blubbers'
list['Bearmattazz'] = 'Honey'
document.write list.blubber
result = (item for item in list when item.match(/(mattazz)/g))
document.write '<br>Res: ', result
钢笔
http://codepen.io/anon/pen/OVrwKO
想要
我想要 mattazz
的正则表达式,例如如果 mattazz
键位于 list
中,则检索值“Honey”。
最佳答案
您正在尝试迭代对象,但您的咖啡语法适用于数组迭代。为此,您需要 of
关键字。 (参见Coffescript.org: Loops and Comprehensions)
list
是一个对象(如键、值存储),因此您要使用 list 的键、值
:
list = {}
list['Bearmattazz'] = 'Honey'
# in case you want to retrieve value
#
result = (value for key, value of list when key.match(/(mattazz)/g))
# in case you want to retrieve key
#
result = (key for key, value of list when key.match(/(mattazz)/g))
document.write '<br>Res: ', result
关于javascript - CoffeeScript RegEx 列表理解与字典对象/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784816/