Last active
December 26, 2015 08:09
-
-
Save walkingice/7120094 to your computer and use it in GitHub Desktop.
check and initialize a variable at the same time in livescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scope = {}; | |
objs = [ | |
{type:'aaa', val:'foo1'}, | |
{type:'bbb', val:'foo2'}, | |
{type:'ccc', val:'foo3'}, | |
{type:'aaa', val:'foo4'}, | |
{type:'bbb', val:'foo5'}, | |
{type:'ccc', val:'foo6'} | |
]; | |
objs.forEach(function(obj) { | |
/* UGLY!! how do we avoid this kind of existence-checking-and-initialization */ | |
if !scope[obj.type] { | |
scope[obj.type] = [] | |
} | |
scope[obj.type].push(obj.name); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from clkao: scope[obj.type] ?= []