Skip to content

Instantly share code, notes, and snippets.

@infacq
Forked from PanosJee/underscore.examples.js
Created October 3, 2013 04:24
Show Gist options
  • Save infacq/6804955 to your computer and use it in GitHub Desktop.
Save infacq/6804955 to your computer and use it in GitHub Desktop.
// 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment