Lo-Dash v0.1.0
_
__.VERSION_.after_.bind_.bindAll_.chain_.chain_.clone_.compact_.compose_.contains_.debounce_.defaults_.defer_.delay_.difference_.escape_.every_.extend_.filter_.find_.first_.flatten_.forEach_.functions_.groupBy_.has_.identity_.indexOf_.initial_.intersection_.invoke_.isArguments_.isArray_.isBoolean_.isDate_.isElement_.isEmpty_.isEqual_.isFinite_.isFunction_.isNaN_.isNull_.isNumber_.isObject_.isRegExp_.isString_.isUndefined_.keys_.last_.lastIndexOf_.map_.max_.memoize_.min_.mixin_.noConflict_.once_.pick_.pluck_.range_.reduce_.reduceRight_.reject_.rest_.result_.shuffle_.size_.some_.sortBy_.sortedIndex_.tap_.template_.throttle_.times_.toArray_.union_.uniq_.uniqueId_.value_.values_.without_.wrap_.zip
_.templateSettings
_.templateSettings_.templateSettings.escape_.templateSettings.evaluate_.templateSettings.interpolate
_
_(value)
The lodash function.
▲
Arguments
value(Mixed): The value to wrap in aLodashinstance.
Returns
(Object): Returns a Lodash instance.
_
_(value)
The lodash function.
▲
_.VERSION
(String): The semantic version number. ▲
_.after(times, func)
Creates a new function that is restricted to executing only after it is called a given number of times.
▲
Arguments
times(Number): The number of times the function must be called before it is executed.func(Function): The function to restrict.
Returns
(Function): Returns the new restricted function.
Example
var renderNotes = _.after(notes.length, render);
_.forEach(notes, function(note) {
note.asyncSave({ 'success': renderNotes });
});
// renderNotes is run once, after all notes have saved.
_.bind(func [, arg1, arg2, ...])
Creates a new function that, when called, invokes func with the this binding of thisArg and prepends additional arguments to those passed to the bound function.
▲
Arguments
func(Function): The function to bind.[arg1, arg2, ...](Mixed): Arguments to prepend to those passed to the bound function.
Returns
(Function): Returns the new bound function.
Example
var func = function(greeting) { return greeting + ': ' + this.name; };
func = _.bind(func, { 'name': 'moe' }, 'hi');
func();
// => 'hi: moe'
_.bindAll(object [, methodName1, methodName2, ...])
Binds methods on the object to the object, overwriting the non-bound method. If no method names are provided, all the function properties of the object will be bound.
▲
Arguments
object(Object): The object to bind and assign the bound methods to.[methodName1, methodName2, ...](Mixed): Method names on the object to bind.
Returns
(Object): Returns the object.
Example
var buttonView = {
'label': 'lodash',
'onClick': function() { alert('clicked: ' + this.label); },
'onHover': function() { console.log('hovering: ' + this.label); }
};
_.bindAll(buttonView);
jQuery('#lodash_button').on('click', buttonView.onClick);
// => When the button is clicked, `this.label` will have the correct value
_.chain()
Extracts the value from a wrapped chainable object. ▲
Returns
(Mixed): Returns the wrapped object.
Example
_([1, 2, 3]).value();
// => [1, 2, 3]
_.chain(value)
Wraps the value in a lodash chainable object.
▲
Arguments
value(Mixed): The value to wrap.
Returns
(Object): Returns the lodash chainable object.
Example
var stooges = [
{ 'name': 'moe', 'age': 40 },
{ 'name': 'larry', 'age': 50 },
{ 'name': 'curly', 'age': 60 }
];
var youngest = _.chain(stooges)
.sortBy(function(stooge) { return stooge.age; })
.map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
.first()
.value();
// => 'moe is 40'
_.clone(value)
Create a shallow clone of the value. Any nested objects or arrays will be assigned by reference and not cloned.
▲
Arguments
value(Mixed): The value to clone.
Returns
(Mixed): Returns the cloned value.
Example
_.clone({ 'name': 'moe' });
// => { 'name': 'moe' };
_.compact(array)
Produces a new array with all falsey values of array removed. The values false, null, 0, "", undefined and NaN are all falsey.
▲
Arguments
array(Array): The array to compact.
Returns
(Array): Returns a new filtered array.
Example
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
_.compose([func1, func2, ...])
Creates a new function that is the composition of the passed functions, where each function consumes the return value of the function that follows. In math terms, composing thefunctions f(), g(), and h() produces f(g(h())).
▲
Arguments
[func1, func2, ...](Mixed): Functions to compose.
Returns
(Function): Returns the new composed function.
Example
var greet = function(name) { return 'hi: ' + name; };
var exclaim = function(statement) { return statement + '!'; };
var welcome = _.compose(exclaim, greet);
welcome('moe');
// => 'hi: moe!'
_.contains(collection, target)
Checks if a given target value is present in a collection using strict equality for comparisons, i.e. ===.
▲
Arguments
collection(Array|Object): The collection to iterate over.target(Mixed): The value to check for.
Returns
(Boolean): Returns true if target value is found, else false.
Example
_.contains([1, 2, 3], 3);
// => true
_.debounce(func, wait, immediate)
Creates a new function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked. Pass true for immediate to cause debounce to invoke the function on the leading, instead of the trailing, edge of the wait timeout.
▲
Arguments
func(Function): The function to debounce.wait(Number): The number of milliseconds to postone.immediate(Boolean): A flag to indicate execution is on the leading edge of the timeout.
Returns
(Function): Returns the new debounced function.
Example
var lazyLayout = _.debounce(calculateLayout, 300);
jQuery(window).on('resize', lazyLayout);
_.defaults(object [, defaults1, defaults2, ..])
Assigns missing properties in object with default values from the defaults objects. As soon as a property is set, additional defaults of the same property will be ignored.
▲
Arguments
object(Object): The object to populate.[defaults1, defaults2, ..](Object): The defaults objects to apply toobject.
Returns
(Object): Returns object.
Example
var iceCream = { 'flavor': 'chocolate' };
_.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'lots' });
// => { 'flavor': 'chocolate', 'sprinkles': 'lots' }
_.defer(func [, arg1, arg2, ...])
Defers invoking the func function until the current call stack has cleared. Additional arguments are passed to func when it is invoked.
▲
Arguments
func(Function): The function to defer.[arg1, arg2, ...](Mixed): Arguments to invoke the function with.
Returns
(Number): Returns the setTimeout timeout id.
Example
_.defer(function() { alert('deferred'); });
// Returns from the function before the alert runs.
_.delay(func, wait [, arg1, arg2, ...])
Invokes the func function after wait milliseconds. Additional arguments are passed func when it is invoked.
▲
Arguments
func(Function): The function to delay.wait(Number): The number of milliseconds to delay execution.[arg1, arg2, ...](Mixed): Arguments to invoke the function with.
Returns
(Number): Returns the setTimeout timeout id.
Example
var log = _.bind(console.log, console);
_.delay(log, 1000, 'logged later');
// => 'logged later' (Appears after one second.)
_.difference(array [, array1, array2, ...])
Produces a new array of array values not present in the other arrays using strict equality for comparisons, i.e. ===.
▲
Arguments
array(Array): The array to process.[array1, array2, ...](Mixed): Arrays to check.
Returns
(Array): Returns a new array of array values not present in the other arrays.
Example
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
// => [1, 3, 4]
_.escape(string)
Escapes a string for insertion into HTML, replacing &, <, >, ", ', and / characters.
▲
Arguments
string(String): The string to escape.
Returns
(String): Returns the escaped string.
Example
_.escape('Curly, Larry & Moe');
// => "Curly, Larry & Moe"
_.every(collection, callback [, thisArg])
Checks if the callback returns truthy for all values of a collection. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Boolean): Returns true if all values pass the callback check, else false.
Example
_.every([true, 1, null, 'yes'], Boolean);
=> false
_.extend(object [, source1, source2, ..])
Copies enumerable properties from the source objects to the destination object. Subsequent sources will overwrite propery assignments of previous sources.
▲
Arguments
object(Object): The destination object.[source1, source2, ..](Object): The source objects.
Returns
(Object): Returns the destination object.
Example
_.extend({ 'name': 'moe' }, { 'age': 40 });
// => { 'name': 'moe', 'age': 40 }
_.filter(collection, callback [, thisArg])
Examines each value in a collection, returning an array of all values the callback returns truthy for. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Array): Returns a new array of values that passed callback check.
Example
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
// => [2, 4, 6]
_.find(collection, callback [, thisArg])
Examines each value in a collection, returning the first one the callback returns truthy for. The function returns as soon as it finds an acceptable value, and does not iterate over the entire collection. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Mixed): Returns the value that passed the callback check, else undefined.
Example
var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
// => 2
_.first(array [, n, guard])
Gets the first value of the array. Pass n to return the first n values of the array.
▲
Arguments
array(Array): The array to query.[n](Number): The number of elements to return.[guard](Object): Internally used to allow this method to work with others like_.mapwithout using their callbackindexargument forn.
Returns
(Mixed): Returns the first value or an array of the first n values of the array.
Example
_.first([5, 4, 3, 2, 1]);
// => 5
_.flatten(array, shallow)
Flattens a nested array (the nesting can be to any depth). If shallow is truthy, array will only be flattened a single level.
▲
Arguments
array(Array): The array to compact.shallow(Boolean): A flag to indicate only flattening a single level.
Returns
(Array): Returns a new flattened array.
Example
_.flatten([1, [2], [3, [[4]]]]);
// => [1, 2, 3, 4];
_.flatten([1, [2], [3, [[4]]]], true);
// => [1, 2, 3, [[4]]];
_.forEach(collection, callback [, thisArg])
Iterates over a collection, executing the callback for each value in the collection. The callback is bound to the thisArg value, if one is passed. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Array, Object): Returns the collection.
Example
_.forEach([1, 2, 3], function(num) { alert(num); });
// => alerts each number in turn...
_.forEach({ 'one': 1, 'two': 2, 'three': 3}, function(num) { alert(num); });
// => alerts each number in turn...
_.functions(object)
Produces a sorted array of the properties, own and inherited, of object that have function values.
▲
Arguments
object(Object): The object to inspect.
Returns
(Array): Returns a new array of property names that have function values.
Example
_.functions(_);
// => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
_.groupBy(collection, callback [, thisArg])
Splits a collection into sets, grouped by the result of running each value through callback. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object). The callback argument may also be the name of a property to group by.
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function|String): The function called per iteration or property name to group by.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Object): Returns an object of grouped values.
Example
_.groupBy([1.3, 2.1, 2.4], function(num) { return Math.floor(num); });
// => { '1': [1.3], '2': [2.1, 2.4] }
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }
_.has(object, property)
Checks if the specified object property exists and is a direct property, instead of an inherited property.
▲
Arguments
object(Object): The object to check.property(String): The property to check for.
Returns
(Boolean): Returns true if key is a direct property, else false.
Example
_.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
// => true
_.identity(value)
This function simply returns the first argument passed to it. Note: It is used throughout Lo-Dash as a default callback. ▲
Arguments
value(Mixed): Any value.
Returns
(Mixed): Returns value.
Example
var moe = { 'name': 'moe' };
moe === _.identity(moe);
// => true
_.indexOf(array, value [, isSorted=false])
Gets the index at which the first occurrence of value is found using strict equality for comparisons, i.e. ===. If the array is already sorted, passing true for isSorted will run a faster binary search.
▲
Arguments
array(Array): The array to search.value(Mixed): The value to search for.[isSorted=false](Boolean): A flag to indicate that thearrayis already sorted.
Returns
(Number): Returns the index of the matched value or -1.
Example
_.indexOf([1, 2, 3], 2);
// => 1
_.initial(array [, n, guard])
Gets all but the last value of the array. Pass n to exclude the last n values from the result.
▲
Arguments
array(Array): The array to query.[n](Number): The number of elements to return.[guard](Object): Internally used to allow this method to work with others like_.mapwithout using their callbackindexargument forn.
Returns
(Array): Returns all but the last value or n values of the array.
Example
_.initial([5, 4, 3, 2, 1]);
// => [5, 4, 3, 2]
_.intersection([array1, array2, ...])
Computes the intersection of all the passed-in arrays. ▲
Arguments
[array1, array2, ...](Mixed): Arrays to process.
Returns
(Array): Returns a new array of unique values, in order, that are present in all of the arrays.
Example
_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
// => [1, 2]
_.invoke(collection, methodName [, arg1, arg2, ...])
Calls the method named by methodName for each value of the collection. Additional arguments will be passed to each invoked method.
▲
Arguments
collection(Array|Object): The collection to iterate over.methodName(String): The name of the method to invoke.[arg1, arg2, ...](Mixed): Arguments to invoke the method with.
Returns
(Array): Returns a new array of values returned from each invoked method.
Example
_.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
// => [[1, 5, 7], [1, 2, 3]]
_.isArguments(value)
Checks if a value is an arguments object.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is an arguments object, else false.
Example
(function() { return _.isArguments(arguments); })(1, 2, 3);
// => true
_.isArguments([1, 2, 3]);
// => false
_.isArray(value)
Checks if a value is an array.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is an array, else false.
Example
(function() { return _.isArray(arguments); })();
// => false
_.isArray([1, 2, 3]);
// => true
_.isBoolean(value)
Checks if a value is a boolean (true or false) value.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a boolean value, else false.
Example
_.isBoolean(null);
// => false
_.isDate(value)
Checks if a value is a date.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a date, else false.
Example
_.isDate(new Date);
// => true
_.isElement(value)
Checks if a value is a DOM element.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a DOM element, else false.
Example
_.isElement(document.body);
// => true
_.isEmpty(value)
Checks if a value is empty. Arrays or strings with a length of 0 and objects with no enumerable own properties are considered "empty".
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is empty, else false.
Example
_.isEmpty([1, 2, 3]);
// => false
_.isEmpty({});
// => true
_.isEqual(a, b [, stack])
Performs a deep comparison between two values to determine if they are equivalent to each other. ▲
Arguments
a(Mixed): The value to compare.b(Mixed): The other value to compare.[stack](Array): Internally used to keep track of "seen" objects to avoid circular references.
Returns
(Boolean): Returns true if the values are equvalent, else false.
Example
var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
moe == clone;
// => false
_.isEqual(moe, clone);
// => true
_.isFinite(value)
Checks if a value is a finite number.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a finite number, else false.
Example
_.isFinite(-101);
// => true
_.isFinite('10');
// => false
_.isFinite(Infinity);
// => false
_.isFunction(value)
Checks if a value is a function.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a function, else false.
Example
_.isFunction(''.concat);
// => true
_.isNaN(value)
Checks if a value is NaN. Note: this is not the same as native isNaN, which will return true for undefined and other values. See http://es5.github.com/#x15.1.2.4.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is NaN, else false.
Example
_.isNaN(NaN);
// => true
_.isNaN(new Number(NaN));
// => true
isNaN(undefined);
// => true
_.isNaN(undefined);
// => false
_.isNull(value)
Checks if a value is null.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is null, else false.
Example
_.isNull(null);
// => true
_.isNull(undefined);
// => false
_.isNumber(value)
Checks if a value is a number.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a number, else false.
Example
_.isNumber(8.4 * 5;
// => true
_.isObject(value)
Checks if a value is an object.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is an object, else false.
Example
_.isObject({});
// => true
_.isObject(1);
// => false
_.isRegExp(value)
Checks if a value is a regular expression.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a regular expression, else false.
Example
_.isRegExp(/moe/);
// => true
_.isString(value)
Checks if a value is a string.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is a string, else false.
Example
_.isString('moe');
// => true
_.isUndefined(value)
Checks if a value is undefined.
▲
Arguments
value(Mixed): The value to check.
Returns
(Boolean): Returns true if the value is undefined, else false.
Example
_.isUndefined(void 0);
// => true
_.keys(object)
Produces an array of the object's enumerable own property names.
▲
Arguments
object(Object): The object to inspect.
Returns
(Array): Returns a new array of property names.
Example
_.keys({ 'one': 1, 'two': 2, 'three': 3 });
// => ['one', 'two', 'three']
_.last(array [, n, guard])
Gets the last value of the array. Pass n to return the lasy n values of the array.
▲
Arguments
array(Array): The array to query.[n](Number): The number of elements to return.[guard](Object): Internally used to allow this method to work with others like_.mapwithout using their callbackindexargument forn.
Returns
(Array): Returns all but the last value or n values of the array.
Example
_.last([5, 4, 3, 2, 1]);
// => 1
_.lastIndexOf(array, value)
Gets the index at which the last occurrence of value is found using strict equality for comparisons, i.e. ===.
▲
Arguments
array(Array): The array to search.value(Mixed): The value to search for.
Returns
(Number): Returns the index of the matched value or -1.
Example
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
// => 4
_.map(collection, callback [, thisArg])
Produces a new array of values by mapping each value in the collection through a transformation callback. The callback is bound to the thisArg value, if one is passed. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Array): Returns a new array of values returned by the callback.
Example
_.map([1, 2, 3], function(num) { return num * 3; });
// => [3, 6, 9]
_.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
// => [3, 6, 9]
_.max(collection [, callback, thisArg])
Retrieves the maximum value of a collection. If callback is passed, it will be executed for each value in the collection to generate the criterion by which the value is ranked. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.[callback](Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Mixed): Returns the maximum value.
Example
var stooges = [
{ 'name': 'moe', 'age': 40 },
{ 'name': 'larry', 'age': 50 },
{ 'name': 'curly', 'age': 60 }
];
_.max(stooges, function(stooge) { return stooge.age; });
// => { 'name': 'curly', 'age': 60 };
_.memoize(func [, resolver])
Creates a new function that memoizes the result of func. If resolver is passed, it will be used to determine the cache key for storing the result based on the arguments passed to the memoized function. By default, the first argument passed to the memoized function is used as the cache key.
▲
Arguments
func(Function): The function to have its output memoized.[resolver](Function): A function used to resolve the cache key.
Returns
(Function): Returns the new memoizing function.
Example
var fibonacci = _.memoize(function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
_.min(collection [, callback, thisArg])
Retrieves the minimum value of a collection. If callback is passed, it will be executed for each value in the collection to generate the criterion by which the value is ranked. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.[callback](Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Mixed): Returns the minimum value.
Example
_.min([10, 5, 100, 2, 1000]);
// => 2
_.mixin(object)
Adds functions properties of object to the lodash function and chainable wrapper.
▲
Arguments
object(Object): The object of function properties to add tolodash.
Example
_.mixin({
'capitalize': function(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
});
_.capitalize('curly');
// => 'Curly'
_('larry').capitalize();
// => 'Larry'
_.noConflict()
Reverts the '_' variable to its previous value and returns a reference to the lodash function.
▲
Returns
(Function): Returns the lodash function.
Example
var lodash = _.noConflict();
_.once(func)
Creates a new function that is restricted to one execution. Repeat calls to the function will return the value of the first call. ▲
Arguments
func(Function): The function to restrict.
Returns
(Function): Returns the new restricted function.
Example
var initialize = _.once(createApplication);
initialize();
initialize();
// Application is only created once.
_.pick(object [, prop1, prop2, ..])
Creates an object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. ▲
Arguments
object(Object): The object to pluck.[prop1, prop2, ..](Object): The properties to pick.
Returns
(Object): Returns an object composed of the picked properties.
Example
_.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age');
// => { 'name': 'moe', 'age': 40 }
_.pluck(collection, property)
Retrieves the value of a specified property from all values in a collection.
▲
Arguments
collection(Array|Object): The collection to iterate over.property(String): The property to pluck.
Returns
(Array): Returns a new array of property values.
Example
var stooges = [
{ 'name': 'moe', 'age': 40 },
{ 'name': 'larry', 'age': 50 },
{ 'name': 'curly', 'age': 60 }
];
_.pluck(stooges, 'name');
// => ['moe', 'larry', 'curly']
_.range([start=0], end [, step=1])
Creates an array of numbers (positive and/or negative) progressing from start up to but not including stop. This method is a port of Python's range() function. See http://docs.python.org/library/functions.html#range.
▲
Arguments
[start=0](Number): The start of the range.end(Number): The end of the range.[step=1](Number): The value to increment or descrement by.
Returns
(Array): Returns a new range array.
Example
_.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);
// => []
_.reduce(collection, callback [, accumulator, thisArg])
Boils down a collection to a single value. The initial state of the reduction is accumulator and each successive step of it should be returned by the callback. The callback is bound to the thisArg value, if one is passed. The callback is invoked with 4 arguments; for arrays they are (accumulator, value, index, array) and for objects they are (accumulator, value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[accumulator](Mixed): Initial value of the accumulator.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Mixed): Returns the accumulated value.
Example
var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; });
// => 6
_.reduceRight(collection, callback [, accumulator, thisArg])
The right-associative version of _.reduce. The callback is bound to the thisArg value, if one is passed. The callback is invoked with 4 arguments; for arrays they are (accumulator, value, index, array) and for objects they are (accumulator, value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[accumulator](Mixed): Initial value of the accumulator.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Mixed): Returns the accumulated value.
Example
var list = [[0, 1], [2, 3], [4, 5]];
var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
// => [4, 5, 2, 3, 0, 1]
_.reject(collection, callback [, thisArg])
The opposite of _.filter, this method returns the values of a collection that callback does not return truthy for. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Array): Returns a new array of values that did not pass the callback check.
Example
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
// => [1, 3, 5]
_.rest(array [, n, guard])
The opposite of _.initial, this method gets all but the first value of the array. Pass n to exclude the first n values from the result.
▲
Arguments
array(Array): The array to query.[n](Number): The number of elements to return.[guard](Object): Internally used to allow this method to work with others like_.mapwithout using their callbackindexargument forn.
Returns
(Array): Returns all but the first value or n values of the array.
Example
_.rest([5, 4, 3, 2, 1]);
// => [4, 3, 2, 1]
_.result(object, property)
Resolves the value of property on object. If the property is a function it will be invoked and its result returned, else the property value is returned.
▲
Arguments
object(Object): The object to inspect.property(String): The property to get the result of.
Returns
(Mixed): Returns the resolved.
Example
var object = {
'cheese': 'crumpets',
'stuff': function() {
return 'nonsense';
}
};
_.result(object, 'cheese');
// => 'crumpets'
_.result(object, 'stuff');
// => 'nonsense'
_.shuffle(collection)
Produces a new array of shuffled collection values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
▲
Arguments
collection(Array|Object): The collection to shuffle.
Returns
(Array): Returns a new shuffled array.
Example
_.shuffle([1, 2, 3, 4, 5, 6]);
// => [4, 1, 6, 3, 5, 2]
_.size(collection)
Gets the number of values in the collection.
▲
Arguments
collection(Array|Object): The collection inspect.
Returns
(Number): Returns the number of values in the collection.
Example
_.size({ 'one': 1, 'two': 2, 'three': 3 });
// => 3
_.some(collection, callback [, thisArg])
Checks if the callback returns truthy for any value of a collection. The function returns as soon as it finds passing value, and does not iterate over the entire collection. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object).
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Boolean): Returns true if any value passes the callback check, else false.
Example
_.some([null, 0, 'yes', false]);
// => true
_.sortBy(collection, callback [, thisArg])
Produces a new sorted array, ranked in ascending order by the results of running each value of a collection through callback. The callback is invoked with 3 arguments; for arrays they are (value, index, array) and for objects they are (value, key, object). The callback argument may also be the name of a property to sort by (e.g. 'length').
▲
Arguments
collection(Array|Object): The collection to iterate over.callback(Function|String): The function called per iteration or property name to sort by.[thisArg](Mixed): Thethisbinding for the callback.
Returns
(Array): Returns a new array of sorted values.
Example
_.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); });
// => [5, 4, 6, 3, 1, 2]
_.sortedIndex(array, value [, callback])
Uses a binary search to determine the smallest index at which the value should be inserted into the collection in order to maintain the sort order of the collection. If callback is passed, it will be executed for each value in the collection to compute their sort ranking. The callback is invoked with 1 argument.
▲
Arguments
array(Array): The array to iterate over.value(Mixed): The value to evaluate.[callback](Function): The function called per iteration.
Returns
(Number): Returns the index at which the value should be inserted into the collection.
Example
_.sortedIndex([10, 20, 30, 40, 50], 35);
// => 3
_.tap(value, interceptor)
Invokes interceptor with the value as the first argument, and then returns value. The primary purpose of this method is to "tap into" a method chain, in order to performoperations on intermediate results within the chain.
▲
Arguments
value(Mixed): The value to pass tocallback.interceptor(Function): The function to invoke.
Returns
(Mixed): Returns value.
Example
_.chain([1,2,3,200])
.filter(function(num) { return num % 2 == 0; })
.tap(alert)
.map(function(num) { return num * num })
.value();
// => // [2, 200] (alerted)
// => [4, 40000]
_.template(text, data, options)
A JavaScript micro-templating method, similar to John Resig's implementation. Lo-Dash templating handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. ▲
Arguments
text(String): The template text.data(Obect): The data object used to populate the text.options(Object): The options object.
Returns
(Function, String): Returns a compiled function when no data object is given, else it returns the interpolated text.
Example
// using compiled template
var compiled = _.template('hello: <%= name %>');
compiled({ 'name': 'moe' });
// => 'hello: moe'
var list = '% _.forEach(people, function(name) { %> <li><%= name %></li> <% }); %>';
_.template(list, { 'people': ['moe', 'curly', 'larry'] });
// => '<li>moe</li><li>curly</li><li>larry</li>'
var template = _.template('<b><%- value %></b>');
template({ 'value': '<script>' });
// => '<b><script></b>'
// using `print`
var compiled = _.template('<% print("Hello " + epithet); %>');
compiled({ 'epithet': 'stooge' });
// => 'Hello stooge.'
// using custom template settings
_.templateSettings = {
'interpolate': /\{\{(.+?)\}\}/g
};
var template = _.template('Hello {{ name }}!');
template({ 'name': 'Mustache' });
// => 'Hello Mustache!'
// using the `variable` option
_.template('<%= data.hasWith %>', { 'hasWith': 'no' }, { 'variable': 'data' });
// => 'no'
// using the `source` property
<script>
JST.project = <%= _.template(jstText).source %>;
</script>
_.throttle(func, wait)
Creates a new function that, when invoked, will only call the original function at most once per every wait milliseconds.
▲
Arguments
func(Function): The function to throttle.wait(Number): The number of milliseconds to throttle executions to.
Returns
(Function): Returns the new throttled function.
Example
var throttled = _.throttle(updatePosition, 100);
jQuery(window).on('scroll', throttled);
_.times(n, callback [, thisArg])
Executes the callback function n times.
▲
Arguments
n(Number): The number of times to execute the callback.callback(Function): The function called per iteration.[thisArg](Mixed): Thethisbinding for the callback.
Example
_.times(3, function() { genie.grantWish(); });
_.toArray(collection)
Converts the collection, into an array. Useful for converting the arguments object.
▲
Arguments
collection(Array|Object): The collection to convert.
Returns
(Array): Returns the new converted array.
Example
(function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
// => [2, 3, 4]
_.union([array1, array2, ...])
Computes the union of the passed-in arrays. ▲
Arguments
[array1, array2, ...](Mixed): Arrays to process.
Returns
(Array): Returns a new array of unique values, in order, that are present in one or more of the arrays.
Example
_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
// => [1, 2, 3, 101, 10]
_.uniq(array [, isSorted=false, callback])
Produces a duplicate-value-free version of the array using strict equality for comparisons, i.e. ===. If the array is already sorted, passing true for isSorted will run a faster algorithm. If callback is passed, each value of array is passed through a transformation callback before uniqueness is computed. The callback is invoked with 3 arguments; (value, index, array).
▲
Arguments
array(Array): The array to process.[isSorted=false](Boolean): A flag to indicate that thearrayis already sorted.[callback](Function): A
Returns
(Array): Returns a duplicate-value-free array.
Example
_.uniq([1, 2, 1, 3, 1, 4]);
// => [1, 2, 3, 4]
_.uniqueId([prefix])
Generates a unique id. If prefix is passed, the id will be appended to it.
▲
Arguments
[prefix](String): The value to prefix the id with.
Returns
(Number, String): Returns a numeric id if no prefix is passed, else a string id may be returned.
Example
_.uniqueId('contact_');
// => 'contact_104'
_.value()
Extracts the value from a wrapped chainable object. ▲
Returns
(Mixed): Returns the wrapped object.
Example
_([1, 2, 3]).value();
// => [1, 2, 3]
_.values(collection)
Produces an array of enumerable own property values of the collection.
▲
Arguments
collection(Array|Object): The collection to inspect.
Returns
(Array): Returns a new array of property values.
Example
_.values({ 'one': 1, 'two': 2, 'three': 3 });
// => [1, 2, 3]
_.without(array [, value1, value2, ...])
Produces a new array with all occurrences of the values removed using strict equality for comparisons, i.e. ===.
▲
Arguments
array(Array): The array to filter.[value1, value2, ...](Mixed): Values to remove.
Returns
(Array): Returns a new filtered array.
Example
_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
// => [2, 3, 4]
_.wrap(func, wrapper [, arg1, arg2, ...])
Create a new function that passes the func function to the wrapper function as its first argument. Additional arguments are appended to those passed to the wrapper function.
▲
Arguments
func(Function): The function to wrap.wrapper(Function): The wrapper function.[arg1, arg2, ...](Mixed): Arguments to append to those passed to the wrapper.
Returns
(Function): Returns the new function.
Example
var hello = function(name) { return 'hello: ' + name; };
hello = _.wrap(hello, function(func) {
return 'before, ' + func('moe') + ', after';
});
hello();
// => 'before, hello: moe, after'
_.zip([array1, array2, ...])
Merges together the values of each of the arrays with the value at the corresponding position. Useful for separate data sources that are coordinated through matching array indexes. For a matrix of nested arrays, _.zip.apply(...) can transpose the matrix in a similar fashion.
▲
Arguments
[array1, array2, ...](Mixed): Arrays to process.
Returns
(Array): Returns a new array of merged arrays.
Example
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
// => [['moe', 30, true], ['larry', 40, false], ['curly', 50, false]]
_.templateSettings
_.templateSettings
(Object): By default, Lo-Dash uses ERB-style template delimiters, change the following template settings to use alternative delimiters. ▲
_.templateSettings.escape
(RegExp): Used to detect data property values to be HTML-escaped.
▲
_.templateSettings.evaluate
(RegExp): Used to detect code to be evaluated. ▲
_.templateSettings.interpolate
(RegExp): Used to detect data property values to inject.
▲