From c23b2ce717ea053ea15ed173ae47b5c2d6726702 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
- You can use Underscore in either an object-oriented or a functional style, - depending on your preference. The following two lines of code are - identical ways to double a list of numbers. -
- -
-_.map([1, 2, 3], function(n){ return n * 2; });
-_([1, 2, 3]).map(function(n){ return n * 2; });
-
- - Using the object-oriented style allows you to chain together methods. Calling - chain on a wrapped object will cause all future method calls to - return wrapped objects as well. When you've finished the computation, - use value to retrieve the final value. Here's an example of chaining - together a map/flatten/reduce, in order to get the word count of - every word in a song. -
- -
-var lyrics = [
- {line : 1, words : "I'm a lumberjack and I'm okay"},
- {line : 2, words : "I sleep all night and I work all day"},
- {line : 3, words : "He's a lumberjack and he's okay"},
- {line : 4, words : "He sleeps all night and he works all day"}
-];
-
-_(lyrics).chain()
- .map(function(line) { return line.words.split(' '); })
- .flatten()
- .reduce({}, function(counts, word) {
- counts[word] = (counts[word] || 0) + 1;
- return counts;
-}).value();
-
-=> {lumberjack : 2, all : 4, night : 2 ... }
-
- - In addition, the - Array prototype's methods - are proxied through the chained Underscore object, so you can slip a - reverse or a push into your chain, and continue to - modify the array. -
-
Collections
@@ -217,8 +172,9 @@ _(lyrics).chain()
Utility
noConflict,
- identity, breakLoop,
- uniqueId, template
+ identity, times,
+ breakLoop, uniqueId,
+ template
@@ -228,6 +184,53 @@ _(lyrics).chain()
+ You can use Underscore in either an object-oriented or a functional style, + depending on your preference. The following two lines of code are + identical ways to double a list of numbers. +
+ +
+_.map([1, 2, 3], function(n){ return n * 2; });
+_([1, 2, 3]).map(function(n){ return n * 2; });
+
+ + Using the object-oriented style allows you to chain together methods. Calling + chain on a wrapped object will cause all future method calls to + return wrapped objects as well. When you've finished the computation, + use value to retrieve the final value. Here's an example of chaining + together a map/flatten/reduce, in order to get the word count of + every word in a song. +
+ +
+var lyrics = [
+ {line : 1, words : "I'm a lumberjack and I'm okay"},
+ {line : 2, words : "I sleep all night and I work all day"},
+ {line : 3, words : "He's a lumberjack and he's okay"},
+ {line : 4, words : "He sleeps all night and he works all day"}
+];
+
+_(lyrics).chain()
+ .map(function(line) { return line.words.split(' '); })
+ .flatten()
+ .reduce({}, function(counts, word) {
+ counts[word] = (counts[word] || 0) + 1;
+ return counts;
+}).value();
+
+=> {lumberjack : 2, all : 4, night : 2 ... }
+
+ + In addition, the + Array prototype's methods + are proxied through the chained Underscore object, so you can slip a + reverse or a push into your chain, and continue to + modify the array. +
+ times_.times(n, iterator)
+
+ Invokes the given iterator function n times.
+
+_(3).times(function(){ genie.grantWish(); });
+
breakLoop_.breakLoop()