Skip to content

Instantly share code, notes, and snippets.

@walkingice
Last active December 26, 2015 08:09
Show Gist options
  • Save walkingice/7120094 to your computer and use it in GitHub Desktop.
Save walkingice/7120094 to your computer and use it in GitHub Desktop.
check and initialize a variable at the same time in livescript
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);
})
@walkingice
Copy link
Author

from clkao: scope[obj.type] ?= []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment