Cheatsheet
Cheatsheet
Cheatsheet
each
(list, iter, [con]) forEach
Array Functions
Object Functions
keys
(object)
Utility Functions
noConflict
()
first
(array, [n])
bind
map
initial
(array, [n])
Bind a function to an object, meaning that whenever the function is called, the value of this will be the object. Optionally, bind arguments to the function to pre-fill them, also known as currying .
values
(object)
Give control of the "_" variable back to its previous owner. Returns a reference to the Underscore object.
identity
(value)
reduce
last
bindAll
(array, [n])
(func, [*methodNames])
functions
(object)
methods
reduceRight
rest
(array, [n])
tail
Binds a number of methods on the object, specified by methodNames , to be run in the context of that object whenever they are invoked. If no methodNames are provided, all of the object's function properties will be bound to it.
Returns the same value that is used as the argument. Used as default iterator.
mixin
(object)
extend
(destination, *sources)
memoize
(func, [hashFunction])
Copy all of the properties in the source objects over to the destination object.
uniqueId
([prefix])
detect
Returns the first found value that passes a truth test ( iter ).
compact
(array)
Returns a copy of the array with all falsy (0, false, null, undefined, "", NaN) values removed.
select
filter
flatten
(array)
Memoizes a given function by caching the computed result. If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key.
defaults
(object, *defaults)
Fill in missing properties in object with default values from the defaults objects. As soon as the property is filled, further defaults will have no effect.
Generate a globally-unique id for client -side models or DOM elements that need one.
template
(templateString, [con])
Compiles JavaScript templates into functions that can be evaluated for rendering.
reject
delay defer
clone
(func, wait, [*args])
(object)
without
(array, [*values]) (func) Defers invoking the function until the current call stack has cleared, similar to using setTimeout with a delay of 0.
Create a shallow -copied clone of the object. Any nested objects or arrays will be copied by reference, not duplicated.
Chaining
chain
()
all
Returns true if all of the values in the list pass the iter truth test.
union
([*arrays])
tap
(object, interceptor)
any
intersection
(list, iter, [con]) some
([*arrays])
throttle
(func, wait)
Returns true if any of the values in the list pass the iter truth test.
difference unique
(array, other)
Returns a throttled version of the function, that, when invoked repeatedly, will only actually call the wrapped function at most once per every wait milliseconds. uniq
Invokes interceptor with the object, and then returns object. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
_([1,2,3,200]).chain(). select(function(x) { return x%2 == 0; }). tap(console.log). map(function(x) { return x*x }). value(); [2, 200] [4, 40000]
Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is used.
var l = [{n : 'sam', age : 25}, {n : 'moe', age : 21}]; var y = _(list).chain() .sortBy(function(s){ return s.age; }) .map(function(s){ return s.n + ' is ' + s.age; }) .first() .value(); "moe is 21"
contains
(list, value)
include
debounce
(func, wait)
value
()
invoke
indexOf
Calls the method named by methodName on each value in the list with passed arguments (if any).
Returns the index at which value can be found in the array, or -1 if value is not present.
Repeated calls to a debounced function will postpone it's execution until after wait milliseconds have elapsed.
isEqual
once
(func)
Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.
pluck
(list, propertyName)
lastIndexOf
(array, value)
Returns the index of the last occurrence of value in the array, or -1 if value is not present.
Creates a version of the function that can only be called one time. Repeated calls to the modified function will have no effect, returning the value from the original call.
Underscore.js Cheatsheet
isEmpty
(object) http://documentcloud.github.com/underscore/ Returns true if object contains no values.
_.isEmpty({}) true
aveic@mail.ru
max min
zip
([*arrays])
after
(count, func)
Merges together the values of each of the arrays with the values at the corresponding position.
_.zip( ['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z'] ) [ ['a', 1, 'x'], ['b', 2, 'y'], ['c', 3, 'z'] ]
Creates a version of the function that will only be run after first being called count times.
isElement
(object)
sortBy
wrap
(func, wrapper)
isArray
(object)
(object)
Returns a sorted copy of list, ranked by the results of running each value through iterator.
range
Returns a list of integers from start to stop , incremented (or decremented) by step , exclusive.
_.range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] _.range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] _.range(0, 30, 5) [0, 5, 10, 15, 20, 25] _.range(0, -10, -1) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] _.range(0) []
Wraps the first function inside of the wrapper function, passing it as the first argument.
isFunction isString
(object)
(object)
Example
example
(arguments) alias
groupBy
compose
(*functions)
(object) (object)
(object) (object)
Returns the composition of a list of functions, where each function consumes the return value of the function that follows. In math terms, composing the functions f() , g() , and h() produces f(g(h())) .
isBoolean isNull
(object)
(object)
=== *
isUndefined
(object)
toArray
(list)
size
(list)
shuffle
(list)