[英]CoffeeScript RegEx ListComprehension with Dictionary-Object / Array
Code 码
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
Pen 钢笔
http://codepen.io/anon/pen/OVrwKO http://codepen.io/anon/pen/OVrwKO
Want 想
I want to regEx for mattazz
, eg retrieve the value "Honey", in case mattazz
key is in list
. 我想为mattazz
使用regEx,例如,如果mattazz
键在list
,则检索值“ Honey”。
You are trying to iterate through an object , but your coffee syntax is for array iteration . 您正在尝试遍历一个对象 ,但是您的coffee语法用于数组迭代 。 You need the of
keyword for that. 您需要为此of
关键字。 (See Coffescript.org: Loops and Comprehensions ) (请参阅Coffescript.org:循环和理解 )
list
is an object (as in key, value store), so you want to use key, value of list
: list
是一个对象(如键,值存储区中的对象),因此您想使用key, value of 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.