Created
July 7, 2011 15:33
-
-
Save PanosJee/1069770 to your computer and use it in GitHub Desktop.
Some nice examples of Underscore.js
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
// 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
Should've used _.chain