<<

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