Skip to content

Instantly share code, notes, and snippets.

@navarroaxel
Created May 2, 2014 15:32
Show Gist options
  • Save navarroaxel/80ea8b5189205f7aca94 to your computer and use it in GitHub Desktop.
Save navarroaxel/80ea8b5189205f7aca94 to your computer and use it in GitHub Desktop.
Basic Example for underscore use in JavaScript
// Option A
var threes = _.map([1, 2, 3], function(num){ return num * 3; });
// threes => [3, 6, 9]
var odds = _.filter(threes, function(num) { return num % 2 != 0; });
// oddss => [3, 9]
// Option B
var odds = _.filter(_.map([1, 2, 3], function(num){ return num * 3; }), function(num) { return num % 2 != 0; });
// Option C
var odds = _.chain([1, 2, 3])
.map(function(num){ return num * 3; })
.filter(function(num) { return num % 2 != 0; })
.value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment