Skip to content

Instantly share code, notes, and snippets.

@PanosJee
Created July 7, 2011 15:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save PanosJee/1069770 to your computer and use it in GitHub Desktop.
Save PanosJee/1069770 to your computer and use it in GitHub Desktop.
Some nice examples of Underscore.js
// Collecting all unique affected app version for a given data set
var jsonStats = [
{app_versions: ['1.2','1.2.3']},
{app_versions: null},
{app_versions: ['1.2','1.3']}
];
var app_versions = _.uniq(_.flatten(_.compact(_.map(jsonStats, function(day){return day.app_versions }))));
// ["1.2", "1.2.3", "1.3"]
// Bye bye stupid for loops!
_.each(app_versions, function(av){ alert(av); })
// Sum up everything
_.reduce(data.total_errors_spline, function(a,b) { return a+b });
// Select only the days that the app versions 1.2 is affected
var dayStats =_.select(jsonStats, function(day){ return _.any(day.app_versions,function(av){ return av==='1.2' }) })
// And you can even display them!
_.template("These versions are affected on day 1: <%= ds[0].app_versions.join(', ') %>")({ds: dayStats })
// "These versions are affected on day 1: 1.2, 1.2.3"
@infacq
Copy link

infacq commented Oct 3, 2013

any gist for _.after. likely to see it couple with _.each

@gberger
Copy link

gberger commented Mar 13, 2014

Should've used _.chain

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