Вы находитесь на странице: 1из 1

Underscore.

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"

of the array. === map(function(x) { return x*x }).


invoke (list, methodName, [*args]) value();
debounce ⇒ [2, 200] value ()
Calls the method named by (func, wait)
methodName on each value in the indexOf (array, value, [isSorted]) ⇒ [4, 40000] Extracts the value of a wrapped
Repeated calls to a debounced
list with passed arguments (if any). Returns the index at which value object.
function will postpone it's
_(obj).value()
can be found in the array, or -1 if execution until after wait isEqual (object, other)
value is not present. milliseconds have elapsed.
pluck (list, propertyName) Performs an optimized deep
comparison between the two
Extracting a list of property values.
lastIndexOf objects, to determine if they
_.pluck([{k: 1}, {k: 2}], 'k') ⇒ [1, 2] (array, value) once (func)
should be considered equal.
Returns the index of the last Creates a version of the function
occurrence of value in the array, that can only be called one time.
max (list, [iter], [con])
or -1 if value is not present. Repeated calls to the modified isEmpty (object)
_.max([{k: 3}, {k: 1}], function(i) {return i.k}) function will have no effect, Returns true if object contains no
⇒3
returning the value from the values.
zip ([*arrays]) original call. _.isEmpty({}) ⇒ true
min (list, [iter], [con]) Merges together the values of each
of the arrays with the values at the
_.min([2, 3], function(i) {return i*i;}) ⇒ 4
corresponding position. after (count, func) isElement (object)
_.zip( ['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z'] ) Creates a version of the function Returns true if object is a DOM
sortBy (list, iter, [con]) ⇒ [ ['a', 1, 'x'], ['b', 2, 'y'], ['c', 3, 'z'] ] that will only be run after first element.
being called count times.
Returns a sorted copy of list,
ranked by the results of running range ([start], stop, [step])
isArray (object)
each value through iterator. Returns a list of integers from start wrap (func, wrapper)
_.sortBy([1, 2, 3, 4, 5], function(x) { return to stop , incremented (or isArguments (object)
Math.sin(x); } ) Wraps the first function inside of
decremented) by step , exclusive. the wrapper function, passing it as
⇒ [5, 4, 3, 1, 2]
_.range(10) the first argument.
⇒ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] isFunction (object)
_.range(1, 11)
groupBy (list, iter, [con])
⇒ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Splits a collection into sets, compose (*functions)
_.range(0, 30, 5) isString (object)
grouped by the result of iter. ⇒ [0, 5, 10, 15, 20, 25] Returns the composition of a list of
_.groupBy([1.3, 2.1, 2.4], function(x) { return _.range(0, -10, -1) functions, where each function isNumber (object)
Math.floor(x); } ) ⇒ { 1: [1.3], 2: [2.1, 2.4] } ⇒ [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] consumes the return value of the
_.range(0) function that follows. In math isBoolean (object)
⇒ [] terms, composing the functions f() ,
Example
toArray (list) size (list) isDate (object)
g() , and h() produces f(g(h())) .
shuffle (list) isRegExp (object)

example (arguments) alias


isNaN (object) con: context forced for an iterator
some_code_examples(); _.size([1, 1]) ⇒ 2
isNull (object) A bit of description.
* === is used for test equality === *
isUndefined (object)

thanks!
aveic@mail.ru

Вам также может понравиться