Underscore - Js
Underscore - Js
js Cheatsheet
PD
F-
XC
hange E
di
t F-
XC
hange E
di
t
PD
or
or
!
!
W
W
O
O
N
N
Y
Y
Collection Functions
U
Array Functions Function :-) Functions Object Functions Utility Functions
U
B
B
to
to
ww
ww
om
om
k
k
(will also work on the arguments object)
lic
lic
C
C
.c
.c
w
w
tr re tr re
.
.
ac ac
k e r- s o ft w a k e r- s o ft w a
each (list, iter, [con]) forEach first (array, [n]) head bind (func, obj, [*args]) keys (object) noConflict ()
iter: function(element, index, list) Returns first (first n) Bind a function to an object, Retrieve all the names of the Give control of the "_" variable
element(s) of an array. meaning that whenever the object's properties. back to its previous owner. Returns
function is called, the value of this a reference to the Underscore
map (list, iter, [con])
will be the object. Optionally, bind object.
_.map([1, 2, 3], function(x){return x*2;}) initial (array, [n]) arguments to the function to pre- values (object)
⇒ [2, 4, 6] Returns a copy of an array fill them, also known as currying . Return all of the values of the
excluding last (last n) element(s). object's properties. identity (value)
reduce (list, iter, m, [con]) inject, foldl Returns the same value that is
bindAll (func, [*methodNames]) used as the argument. Used as
iter: function(memo, el), m: memo last (array, [n]) functions (object) methods
Binds a number of methods on the default iterator.
_.reduce([2, 3], function(m, i) {return m+i;}, 0)
⇒6 Returns last (last n) element(s) object, specified by methodNames , Returns a sorted list of the names
from an array. to be run in the context of that of every method in an object.
object whenever they are invoked. mixin (object)
reduceRight (list, iter, m, [con]) foldr If no methodNames are provided, Allows you to extend Underscore
rest (array, [n]) tail all of the object's function extend (destination, *sources) with your own utility functions.
Similar to reduce , but works in
opposite direction. Returns a copy of an array properties will be bound to it. Copy all of the properties in the
excluding first (first n) element(s). source objects over to the
destination object. uniqueId ([prefix])
detect (list, iter, [con]) memoize (func, [hashFunction]) Generate a globally-unique id for
compact (array) Memoizes a given function by client -side models or DOM
Returns the first found value that
Returns a copy of the array with all caching the computed result. If defaults (object, *defaults) elements that need one.
passes a truth test ( iter ).
falsy (0, false, null, undefined, "", passed an optional hashFunction, it Fill in missing properties in object
NaN) values removed. will be used to compute the hash with default values from the
select (list, iter, [con]) filter key for storing the result, based on defaults objects. As soon as the template (templateString, [con])
_.select([1, 2, 3], function(x) {return x<3;} ) the arguments to the original property is filled, further defaults Compiles JavaScript templates into
⇒ [1, 2]
flatten (array) function. The default hashFunction will have no effect. functions that can be evaluated for
Flattens a nested array. just uses the first argument to the rendering.
_.flatten([1, 2, [[3], 4]]) ⇒ [1, 2, 3, 4] memoized function as the key.
reject(list, iter, [con]) clone (object)
Opposite of select.
without
Create a shallow -copied clone of Chaining
(array, [*values]) delay (func, wait, [*args]) the object. Any nested objects or
Copy of the array with all passed arrays will be copied by reference,
all (list, iter, [con]) every values removed. === not duplicated.
Returns true if all of the values in defer (func) chain ()
the list pass the iter truth test. Defers invoking the function until
union ([*arrays]) tap (object, interceptor) Returns a wrapped object. Calling
the current call stack has cleared, methods on this object will
similar to using setTimeout with a Invokes interceptor with the object,
any (list, iter, [con]) some continue to return wrapped objects
delay of 0. and then returns object. The
intersection ([*arrays]) until value is used.
Returns true if any of the values in primary purpose of this method is var l = [{n : 'sam', age : 25}, {n : 'moe', age :
the list pass the iter truth test. to "tap into" a method chain, in 21}];
throttle (func, wait) order to perform operations on var y = _(list).chain()
difference (array, other) Returns a throttled version of the intermediate results within the .sortBy(function(s){ return s.age; })
contains (list, value) include
function, that, when invoked chain. .map(function(s){ return s.n + ' is ' + s.age; })
Returns true if the value is present repeatedly, will only actually call _([1,2,3,200]).chain(). .first()
in the list. === unique (array, [isSorted], [iter]) uniq the wrapped function at most once select(function(x) { return x%2 == 0; }). .value();
Produces a duplicate-free version per every wait milliseconds. tap(console.log). ⇒ "moe is 21"
thanks!
[email protected]