- \n
_.compact
\n_.difference
\n_.drop
\n_.first
\n_.flatten
\n_.head
\n_.indexOf
\n_.initial
\n_.intersection
\n_.last
\n_.lastIndexOf
\n_.object
\n_.range
\n_.rest
\n_.sortedIndex
\n_.tail
\n_.take
\n_.union
\n_.uniq
\n_.unique
\n_.without
\n_.zip
\n
- \n
_.all
\n_.any
\n_.at
\n_.collect
\n_.contains
\n_.countBy
\n_.detect
\n_.each
\n_.every
\n_.filter
\n_.find
\n_.foldl
\n_.foldr
\n_.forEach
\n_.groupBy
\n_.include
\n_.inject
\n_.invoke
\n_.map
\n_.max
\n_.min
\n_.pluck
\n_.reduce
\n_.reduceRight
\n_.reject
\n_.select
\n_.shuffle
\n_.size
\n_.some
\n_.sortBy
\n_.toArray
\n_.where
\n
- \n
_.after
\n_.bind
\n_.bindAll
\n_.bindKey
\n_.compose
\n_.debounce
\n_.defer
\n_.delay
\n_.memoize
\n_.once
\n_.partial
\n_.partialRight
\n_.throttle
\n_.wrap
\n
- \n
_.assign
\n_.clone
\n_.cloneDeep
\n_.defaults
\n_.extend
\n_.forIn
\n_.forOwn
\n_.functions
\n_.has
\n_.invert
\n_.isArguments
\n_.isArray
\n_.isBoolean
\n_.isDate
\n_.isElement
\n_.isEmpty
\n_.isEqual
\n_.isFinite
\n_.isFunction
\n_.isNaN
\n_.isNull
\n_.isNumber
\n_.isObject
\n_.isPlainObject
\n_.isRegExp
\n_.isString
\n_.isUndefined
\n_.keys
\n_.merge
\n_.methods
\n_.omit
\n_.pairs
\n_.pick
\n_.values
\n
- \n
_.escape
\n_.identity
\n_.mixin
\n_.noConflict
\n_.random
\n_.result
\n_.template
\n_.times
\n_.unescape
\n_.uniqueId
\n
- \n
_.VERSION
\n_.templateSettings
\n_.templateSettings.escape
\n_.templateSettings.evaluate
\n_.templateSettings.interpolate
\n_.templateSettings.variable
\n_.templateSettings.imports
\n
Creates an array with all falsey values of array
removed. The values false
, null
, 0
, \"\"
, undefined
and NaN
are all falsey.
- \n
array
(Array): The array to compact. \n
(Array): Returns a new filtered array.
\n\n_.compact([0, 1, false, 2, '', 3]);\n// => [1, 2, 3]
\n\n\n\n\n
Creates an array of array
elements not present in the other arrays using strict equality for comparisons, i.e. ===
.
- \n
array
(Array): The array to process. \n[array1, array2, ...]
(Array): Arrays to check. \n
(Array): Returns a new array of array
elements not present in the other arrays.
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);\n// => [1, 3, 4]
\n\n\n\n\n
Gets the first element of the array
. If a number n
is passed, the first n
elements of the array
are returned. If a callback
function is passed, the first elements the callback
returns truthy for are returned. The callback
is bound to thisArg
and invoked with three arguments; (value, index, array).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
head, take
\n\n- \n
array
(Array): The array to query. \n[callback|n]
(Function|Object|Number|String): The function called per element or the number of elements to return. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the first element(s) of array
.
_.first([1, 2, 3]);\n// => 1\n\n_.first([1, 2, 3], 2);\n// => [1, 2]\n\n_.first([1, 2, 3], function(num) {\n return num < 3;\n});\n// => [1, 2]\n\nvar food = [\n { 'name': 'banana', 'organic': true },\n { 'name': 'beet', 'organic': false },\n];\n\n// using \"_.pluck\" callback shorthand\n_.first(food, 'organic');\n// => [{ 'name': 'banana', 'organic': true }]\n\nvar food = [\n { 'name': 'apple', 'type': 'fruit' },\n { 'name': 'banana', 'type': 'fruit' },\n { 'name': 'beet', 'type': 'vegetable' }\n];\n\n// using \"_.where\" callback shorthand\n_.first(food, { 'type': 'fruit' });\n// => [{ 'name': 'apple', 'type': 'fruit' }, { 'name': 'banana', 'type': 'fruit' }]
\n\n\n\n\n
Flattens a nested array (the nesting can be to any depth). If shallow
is truthy, array
will only be flattened a single level.
- \n
array
(Array): The array to compact. \nshallow
(Boolean): A flag to indicate only flattening a single level. \n
(Array): Returns a new flattened array.
\n\n_.flatten([1, [2], [3, [[4]]]]);\n// => [1, 2, 3, 4];\n\n_.flatten([1, [2], [3, [[4]]]], true);\n// => [1, 2, 3, [[4]]];
\n\n\n\n\n
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 fromIndex
will run a faster binary search.
- \n
array
(Array): The array to search. \nvalue
(Mixed): The value to search for. \n[fromIndex=0]
(Boolean|Number): The index to search from ortrue
to perform a binary search on a sortedarray
. \n
(Number): Returns the index of the matched value or -1
.
_.indexOf([1, 2, 3, 1, 2, 3], 2);\n// => 1\n\n_.indexOf([1, 2, 3, 1, 2, 3], 2, 3);\n// => 4\n\n_.indexOf([1, 1, 2, 2, 3, 3], 2, true);\n// => 2
\n\n\n\n\n
Gets all but the last element of array
. If a number n
is passed, the last n
elements are excluded from the result. If a callback
function is passed, the last elements the callback
returns truthy for are excluded from the result. The callback
is bound to thisArg
and invoked with three arguments; (value, index, array).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
array
(Array): The array to query. \n[callback|n=1]
(Function|Object|Number|String): The function called per element or the number of elements to exclude. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a slice of array
.
_.initial([1, 2, 3]);\n// => [1, 2]\n\n_.initial([1, 2, 3], 2);\n// => [1]\n\n_.initial([1, 2, 3], function(num) {\n return num > 1;\n});\n// => [1]\n\nvar food = [\n { 'name': 'beet', 'organic': false },\n { 'name': 'carrot', 'organic': true }\n];\n\n// using \"_.pluck\" callback shorthand\n_.initial(food, 'organic');\n// => [{ 'name': 'beet', 'organic': false }]\n\nvar food = [\n { 'name': 'banana', 'type': 'fruit' },\n { 'name': 'beet', 'type': 'vegetable' },\n { 'name': 'carrot', 'type': 'vegetable' }\n];\n\n// using \"_.where\" callback shorthand\n_.initial(food, { 'type': 'vegetable' });\n// => [{ 'name': 'banana', 'type': 'fruit' }]
\n\n\n\n\n
Computes the intersection of all the passed-in arrays using strict equality for comparisons, i.e. ===
.
- \n
[array1, array2, ...]
(Array): Arrays to process. \n
(Array): Returns a new array of unique elements that are present in all of the arrays.
\n\n_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);\n// => [1, 2]
\n\n\n\n\n
Gets the last element of the array
. If a number n
is passed, the last n
elements of the array
are returned. If a callback
function is passed, the last elements the callback
returns truthy for are returned. The callback
is bound to thisArg
and invoked with three arguments; (value, index, array).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
array
(Array): The array to query. \n[callback|n]
(Function|Object|Number|String): The function called per element or the number of elements to return. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the last element(s) of array
.
_.last([1, 2, 3]);\n// => 3\n\n_.last([1, 2, 3], 2);\n// => [2, 3]\n\n_.last([1, 2, 3], function(num) {\n return num > 1;\n});\n// => [2, 3]\n\nvar food = [\n { 'name': 'beet', 'organic': false },\n { 'name': 'carrot', 'organic': true }\n];\n\n// using \"_.pluck\" callback shorthand\n_.last(food, 'organic');\n// => [{ 'name': 'carrot', 'organic': true }]\n\nvar food = [\n { 'name': 'banana', 'type': 'fruit' },\n { 'name': 'beet', 'type': 'vegetable' },\n { 'name': 'carrot', 'type': 'vegetable' }\n];\n\n// using \"_.where\" callback shorthand\n_.last(food, { 'type': 'vegetable' });\n// => [{ 'name': 'beet', 'type': 'vegetable' }, { 'name': 'carrot', 'type': 'vegetable' }]
\n\n\n\n\n
Gets the index at which the last occurrence of value
is found using strict equality for comparisons, i.e. ===
. If fromIndex
is negative, it is used as the offset from the end of the collection.
- \n
array
(Array): The array to search. \nvalue
(Mixed): The value to search for. \n[fromIndex=array.length-1]
(Number): The index to search from. \n
(Number): Returns the index of the matched value or -1
.
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);\n// => 4\n\n_.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);\n// => 1
\n\n\n\n\n
Creates an object composed from arrays of keys
and values
. Pass either a single two dimensional array, i.e. [[key1, value1], [key2, value2]]
, or two arrays, one of keys
and one of corresponding values
.
- \n
keys
(Array): The array of keys. \n[values=[]]
(Array): The array of values. \n
(Object): Returns an object composed of the given keys and corresponding values.
\n\n_.object(['moe', 'larry'], [30, 40]);\n// => { 'moe': 30, 'larry': 40 }
\n\n\n\n\n
Creates an array of numbers (positive and/or negative) progressing from start
up to but not including end
.
- \n
[start=0]
(Number): The start of the range. \nend
(Number): The end of the range. \n[step=1]
(Number): The value to increment or descrement by. \n
(Array): Returns a new range array.
\n\n_.range(10);\n// => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n_.range(1, 11);\n// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\n_.range(0, 30, 5);\n// => [0, 5, 10, 15, 20, 25]\n\n_.range(0, -10, -1);\n// => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n\n_.range(0);\n// => []
\n\n\n\n\n
The opposite of _.initial
, this method gets all but the first value of array
. If a number n
is passed, the first n
values are excluded from the result. If a callback
function is passed, the first elements the callback
returns truthy for are excluded from the result. The callback
is bound to thisArg
and invoked with three arguments; (value, index, array).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
drop, tail
\n\n- \n
array
(Array): The array to query. \n[callback|n=1]
(Function|Object|Number|String): The function called per element or the number of elements to exclude. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a slice of array
.
_.rest([1, 2, 3]);\n// => [2, 3]\n\n_.rest([1, 2, 3], 2);\n// => [3]\n\n_.rest([1, 2, 3], function(num) {\n return num < 3;\n});\n// => [3]\n\nvar food = [\n { 'name': 'banana', 'organic': true },\n { 'name': 'beet', 'organic': false },\n];\n\n// using \"_.pluck\" callback shorthand\n_.rest(food, 'organic');\n// => [{ 'name': 'beet', 'organic': false }]\n\nvar food = [\n { 'name': 'apple', 'type': 'fruit' },\n { 'name': 'banana', 'type': 'fruit' },\n { 'name': 'beet', 'type': 'vegetable' }\n];\n\n// using \"_.where\" callback shorthand\n_.rest(food, { 'type': 'fruit' });\n// => [{ 'name': 'beet', 'type': 'vegetable' }]
\n\n\n\n\n
Uses a binary search to determine the smallest index at which the value
should be inserted into array
in order to maintain the sort order of the sorted array
. If callback
is passed, it will be executed for value
and each element in array
to compute their sort ranking. The callback
is bound to thisArg
and invoked with one argument; (value).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
array
(Array): The array to iterate over. \nvalue
(Mixed): The value to evaluate. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Number): Returns the index at which the value should be inserted into array
.
_.sortedIndex([20, 30, 50], 40);\n// => 2\n\n// using \"_.pluck\" callback shorthand\n_.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');\n// => 2\n\nvar dict = {\n 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }\n};\n\n_.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {\n return dict.wordToNumber[word];\n});\n// => 2\n\n_.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {\n return this.wordToNumber[word];\n}, dict);\n// => 2
\n\n\n\n\n
Computes the union of the passed-in arrays using strict equality for comparisons, i.e. ===
.
- \n
[array1, array2, ...]
(Array): Arrays to process. \n
(Array): Returns a new array of unique values, in order, that are present in one or more of the arrays.
\n\n_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);\n// => [1, 2, 3, 101, 10]
\n\n\n\n\n
Creates 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 element of array
is passed through a callbackbefore uniqueness is computed. The
callbackis bound to
thisArg` and invoked with three arguments; (value, index, array).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
unique
\n\n- \n
array
(Array): The array to process. \n[isSorted=false]
(Boolean): A flag to indicate that thearray
is already sorted. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a duplicate-value-free array.
\n\n_.uniq([1, 2, 1, 3, 1]);\n// => [1, 2, 3]\n\n_.uniq([1, 1, 2, 2, 3], true);\n// => [1, 2, 3]\n\n_.uniq([1, 2, 1.5, 3, 2.5], function(num) { return Math.floor(num); });\n// => [1, 2, 3]\n\n_.uniq([1, 2, 1.5, 3, 2.5], function(num) { return this.floor(num); }, Math);\n// => [1, 2, 3]\n\n// using \"_.pluck\" callback shorthand\n_.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');\n// => [{ 'x': 1 }, { 'x': 2 }]
\n\n\n\n\n
Creates an array with all occurrences of the passed values removed using strict equality for comparisons, i.e. ===
.
- \n
array
(Array): The array to filter. \n[value1, value2, ...]
(Mixed): Values to remove. \n
(Array): Returns a new filtered array.
\n\n_.without([1, 2, 1, 0, 3, 1, 4], 0, 1);\n// => [2, 3, 4]
\n\n\n\n\n
Groups the elements of each array at their corresponding indexes. 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.
- \n
[array1, array2, ...]
(Array): Arrays to process. \n
(Array): Returns a new array of grouped elements.
\n\n_.zip(['moe', 'larry'], [30, 40], [true, false]);\n// => [['moe', 30, true], ['larry', 40, false]]
\n\n\n\n\n\n\n\n
Creates a lodash
object, that wraps the given value
, to enable method chaining.
In addition to Lo-Dash methods, wrappers also have the following Array
methods:
\nconcat
, join
, pop
, push
, reverse
, shift
, slice
, sort
, splice
, and unshift
The chainable wrapper functions are:
\nafter
, assign
, bind
, bindAll
, bindKey
, chain
, compact
, compose
, concat
, countBy
, debounce
, defaults
, defer
, delay
, difference
, filter
, flatten
, forEach
, forIn
, forOwn
, functions
, groupBy
, initial
, intersection
, invert
, invoke
, keys
, map
, max
, memoize
, merge
, min
, object
, omit
, once
, pairs
, partial
, partialRight
, pick
, pluck
, push
, range
, reject
, rest
, reverse
, shuffle
, slice
, sort
, sortBy
, splice
, tap
, throttle
, times
, toArray
, union
, uniq
, unshift
, values
, where
, without
, wrap
, and zip
The non-chainable wrapper functions are:
\nclone
, cloneDeep
, contains
, escape
, every
, find
, has
, identity
, indexOf
, isArguments
, isArray
, isBoolean
, isDate
, isElement
, isEmpty
, isEqual
, isFinite
, isFunction
, isNaN
, isNull
, isNumber
, isObject
, isPlainObject
, isRegExp
, isString
, isUndefined
, join
, lastIndexOf
, mixin
, noConflict
, pop
, random
, reduce
, reduceRight
, result
, shift
, size
, some
, sortedIndex
, template
, unescape
, and uniqueId
The wrapper functions first
and last
return wrapped values when n
is passed, otherwise they return unwrapped values.
- \n
value
(Mixed): The value to wrap in alodash
instance. \n
(Object): Returns a lodash
instance.
\n\n\n\n\n
Invokes interceptor
with the value
as the first argument, and then returns value
. The purpose of this method is to \"tap into\" a method chain, in order to perform operations on intermediate results within the chain.
- \n
value
(Mixed): The value to pass tointerceptor
. \ninterceptor
(Function): The function to invoke. \n
(Mixed): Returns value
.
_([1, 2, 3, 4])\n .filter(function(num) { return num % 2 == 0; })\n .tap(alert)\n .map(function(num) { return num * num; })\n .value();\n// => // [2, 4] (alerted)\n// => [4, 16]
\n\n\n\n\n
Produces the toString
result of the wrapped value.
(String): Returns the string result.
\n\n_([1, 2, 3]).toString();\n// => '1,2,3'
\n\n\n\n\n
Extracts the wrapped value.
\n\nvalue
\n\n(Mixed): Returns the wrapped value.
\n\n_([1, 2, 3]).valueOf();\n// => [1, 2, 3]
\n\n\n\n\n\n\n\n
Creates an array of elements from the specified index(es), or keys, of the collection
. Indexes may be specified as individual arguments or as arrays of indexes.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[index1, index2, ...]
(Array|Number|String): The index(es) ofcollection
to retrieve, either as individual arguments or arrays. \n
(Array): Returns a new array of elements corresponding to the provided indexes.
\n\n_.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);\n// => ['a', 'c', 'e']\n\n_.at(['moe', 'larry', 'curly'], 0, 2);\n// => ['moe', 'curly']
\n\n\n\n\n
Checks if a given target
element is present in a collection
using strict equality for comparisons, i.e. ===
. If fromIndex
is negative, it is used as the offset from the end of the collection.
include
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \ntarget
(Mixed): The value to check for. \n[fromIndex=0]
(Number): The index to search from. \n
(Boolean): Returns true
if the target
element is found, else false
.
_.contains([1, 2, 3], 1);\n// => true\n\n_.contains([1, 2, 3], 1, 2);\n// => false\n\n_.contains({ 'name': 'moe', 'age': 40 }, 'moe');\n// => true\n\n_.contains('curly', 'ur');\n// => true
\n\n\n\n\n
Creates an object composed of keys returned from running each element of the collection
through the given callback
. The corresponding value of each key is the number of times the key was returned by the callback
. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns the composed aggregate object.
\n\n_.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });\n// => { '4': 1, '6': 2 }\n\n_.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);\n// => { '4': 1, '6': 2 }\n\n_.countBy(['one', 'two', 'three'], 'length');\n// => { '3': 2, '5': 1 }
\n\n\n\n\n
Checks if the callback
returns a truthy value for all elements of a collection
. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
all
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Boolean): Returns true
if all elements pass the callback check, else false
.
_.every([true, 1, null, 'yes'], Boolean);\n// => false\n\nvar stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n// using \"_.pluck\" callback shorthand\n_.every(stooges, 'age');\n// => true\n\n// using \"_.where\" callback shorthand\n_.every(stooges, { 'age': 50 });\n// => false
\n\n\n\n\n
Examines each element in a collection
, returning an array of all elements the callback
returns truthy for. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
select
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a new array of elements that passed the callback check.
\n\nvar evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });\n// => [2, 4, 6]\n\nvar food = [\n { 'name': 'apple', 'organic': false, 'type': 'fruit' },\n { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }\n];\n\n// using \"_.pluck\" callback shorthand\n_.filter(food, 'organic');\n// => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]\n\n// using \"_.where\" callback shorthand\n_.filter(food, { 'type': 'fruit' });\n// => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]
\n\n\n\n\n
Examines each element in a collection
, returning the first that the callback
returns truthy for. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
detect
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the element that passed the callback check, else undefined
.
var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });\n// => 2\n\nvar food = [\n { 'name': 'apple', 'organic': false, 'type': 'fruit' },\n { 'name': 'banana', 'organic': true, 'type': 'fruit' },\n { 'name': 'beet', 'organic': false, 'type': 'vegetable' },\n { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }\n];\n\n// using \"_.where\" callback shorthand\nvar veggie = _.find(food, { 'type': 'vegetable' });\n// => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }\n\n// using \"_.pluck\" callback shorthand\nvar healthy = _.find(food, 'organic');\n// => { 'name': 'banana', 'organic': true, 'type': 'fruit' }
\n\n\n\n\n
Iterates over a collection
, executing the callback
for each element in the collection
. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection). Callbacks may exit iteration early by explicitly returning false
.
each
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function): The function called per iteration. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array, Object, String): Returns collection
.
_([1, 2, 3]).forEach(alert).join(',');\n// => alerts each number and returns '1,2,3'\n\n_.forEach({ 'one': 1, 'two': 2, 'three': 3 }, alert);\n// => alerts each number value (order is not guaranteed)
\n\n\n\n\n
Creates an object composed of keys returned from running each element of the collection
through the callback
. The corresponding value of each key is an array of elements passed to callback
that returned the key. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns the composed aggregate object.
\n\n_.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });\n// => { '4': [4.2], '6': [6.1, 6.4] }\n\n_.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);\n// => { '4': [4.2], '6': [6.1, 6.4] }\n\n// using \"_.pluck\" callback shorthand\n_.groupBy(['one', 'two', 'three'], 'length');\n// => { '3': ['one', 'two'], '5': ['three'] }
\n\n\n\n\n
Invokes the method named by methodName
on each element in the collection
, returning an array of the results of each invoked method. Additional arguments will be passed to each invoked method. If methodName
is a function, it will be invoked for, and this
bound to, each element in the collection
.
- \n
collection
(Array|Object|String): The collection to iterate over. \nmethodName
(Function|String): The name of the method to invoke or the function invoked per iteration. \n[arg1, arg2, ...]
(Mixed): Arguments to invoke the method with. \n
(Array): Returns a new array of the results of each invoked method.
\n\n_.invoke([[5, 1, 7], [3, 2, 1]], 'sort');\n// => [[1, 5, 7], [1, 2, 3]]\n\n_.invoke([123, 456], String.prototype.split, '');\n// => [['1', '2', '3'], ['4', '5', '6']]
\n\n\n\n\n
Creates an array of values by running each element in the collection
through the callback
. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
collect
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a new array of the results of each callback
execution.
_.map([1, 2, 3], function(num) { return num * 3; });\n// => [3, 6, 9]\n\n_.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });\n// => [3, 6, 9] (order is not guaranteed)\n\nvar stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n// using \"_.pluck\" callback shorthand\n_.map(stooges, 'name');\n// => ['moe', 'larry']
\n\n\n\n\n
Retrieves the maximum value of an array
. If callback
is passed, it will be executed for each value in the array
to generate the criterion by which the value is ranked. The callback
is bound to thisArg
and invoked with three arguments; (value, index, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the maximum value.
\n\n_.max([4, 2, 8, 6]);\n// => 8\n\nvar stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n_.max(stooges, function(stooge) { return stooge.age; });\n// => { 'name': 'larry', 'age': 50 };\n\n// using \"_.pluck\" callback shorthand\n_.max(stooges, 'age');\n// => { 'name': 'larry', 'age': 50 };
\n\n\n\n\n
Retrieves the minimum value of an array
. If callback
is passed, it will be executed for each value in the array
to generate the criterion by which the value is ranked. The callback
is bound to thisArg
and invoked with three arguments; (value, index, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the minimum value.
\n\n_.min([4, 2, 8, 6]);\n// => 2\n\nvar stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n_.min(stooges, function(stooge) { return stooge.age; });\n// => { 'name': 'moe', 'age': 40 };\n\n// using \"_.pluck\" callback shorthand\n_.min(stooges, 'age');\n// => { 'name': 'moe', 'age': 40 };
\n\n\n\n\n
Retrieves the value of a specified property from all elements in the collection
.
- \n
collection
(Array|Object|String): The collection to iterate over. \nproperty
(String): The property to pluck. \n
(Array): Returns a new array of property values.
\n\nvar stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n_.pluck(stooges, 'name');\n// => ['moe', 'larry']
\n\n\n\n\n
Reduces a collection
to a value that is the accumulated result of running each element in the collection
through the callback
, where each successive callback
execution consumes the return value of the previous execution. If accumulator
is not passed, the first element of the collection
will be used as the initial accumulator
value. The callback
is bound to thisArg
and invoked with four arguments; (accumulator, value, index|key, collection).
foldl, inject
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function): The function called per iteration. \n[accumulator]
(Mixed): Initial value of the accumulator. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the accumulated value.
\n\nvar sum = _.reduce([1, 2, 3], function(sum, num) {\n return sum + num;\n});\n// => 6\n\nvar mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {\n result[key] = num * 3;\n return result;\n}, {});\n// => { 'a': 3, 'b': 6, 'c': 9 }
\n\n\n\n\n
This method is similar to _.reduce
, except that it iterates over a collection
from right to left.
foldr
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function): The function called per iteration. \n[accumulator]
(Mixed): Initial value of the accumulator. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the accumulated value.
\n\nvar list = [[0, 1], [2, 3], [4, 5]];\nvar flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);\n// => [4, 5, 2, 3, 0, 1]
\n\n\n\n\n
The opposite of _.filter
, this method returns the elements of a collection
that callback
does not return truthy for.
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a new array of elements that did not pass the callback check.
\n\nvar odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });\n// => [1, 3, 5]\n\nvar food = [\n { 'name': 'apple', 'organic': false, 'type': 'fruit' },\n { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }\n];\n\n// using \"_.pluck\" callback shorthand\n_.reject(food, 'organic');\n// => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]\n\n// using \"_.where\" callback shorthand\n_.reject(food, { 'type': 'fruit' });\n// => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
\n\n\n\n\n
Creates an array of shuffled array
values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
- \n
collection
(Array|Object|String): The collection to shuffle. \n
(Array): Returns a new shuffled collection.
\n\n_.shuffle([1, 2, 3, 4, 5, 6]);\n// => [4, 1, 6, 3, 5, 2]
\n\n\n\n\n
Gets the size of the collection
by returning collection.length
for arrays and array-like objects or the number of own enumerable properties for objects.
- \n
collection
(Array|Object|String): The collection to inspect. \n
(Number): Returns collection.length
or number of own enumerable properties.
_.size([1, 2]);\n// => 2\n\n_.size({ 'one': 1, 'two': 2, 'three': 3 });\n// => 3\n\n_.size('curly');\n// => 5
\n\n\n\n\n
Checks if the callback
returns a truthy value for any element of a collection
. The function returns as soon as it finds passing value, and does not iterate over the entire collection
. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
any
\n\n- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Boolean): Returns true
if any element passes the callback check, else false
.
_.some([null, 0, 'yes', false], Boolean);\n// => true\n\nvar food = [\n { 'name': 'apple', 'organic': false, 'type': 'fruit' },\n { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }\n];\n\n// using \"_.pluck\" callback shorthand\n_.some(food, 'organic');\n// => true\n\n// using \"_.where\" callback shorthand\n_.some(food, { 'type': 'meat' });\n// => false
\n\n\n\n\n
Creates an array of elements, sorted in ascending order by the results of running each element in the collection
through the callback
. This method performs a stable sort, that is, it will preserve the original sort order of equal elements. The callback
is bound to thisArg
and invoked with three arguments; (value, index|key, collection).
If a property name is passed for callback
, the created \"_.pluck\" style callback will return the property value of the given element.
If an object is passed for callback
, the created \"_.where\" style callback will return true
for elements that have the propeties of the given object, else false
.
- \n
collection
(Array|Object|String): The collection to iterate over. \n[callback=identity]
(Function|Object|String): The function called per iteration. If a property name or object is passed, it will be used to create a \".pluck\" or \".where\" style callback, respectively. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a new array of sorted elements.
\n\n_.sortBy([1, 2, 3], function(num) { return Math.sin(num); });\n// => [3, 1, 2]\n\n_.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);\n// => [3, 1, 2]\n\n// using \"_.pluck\" callback shorthand\n_.sortBy(['banana', 'strawberry', 'apple'], 'length');\n// => ['apple', 'banana', 'strawberry']
\n\n\n\n\n
Converts the collection
to an array.
- \n
collection
(Array|Object|String): The collection to convert. \n
(Array): Returns the new converted array.
\n\n(function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);\n// => [2, 3, 4]
\n\n\n\n\n
Examines each element in a collection
, returning an array of all elements that have the given properties
. When checking properties
, this method performs a deep comparison between values to determine if they are equivalent to each other.
- \n
collection
(Array|Object|String): The collection to iterate over. \nproperties
(Object): The object of property values to filter by. \n
(Array): Returns a new array of elements that have the given properties
.
var stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\n_.where(stooges, { 'age': 40 });\n// => [{ 'name': 'moe', 'age': 40 }]
\n\n\n\n\n\n\n\n
Creates a function that is restricted to executing func
only after it is called n
times. The func
is executed with the this
binding of the created function.
- \n
n
(Number): The number of times the function must be called before it is executed. \nfunc
(Function): The function to restrict. \n
(Function): Returns the new restricted function.
\n\nvar renderNotes = _.after(notes.length, render);\n_.forEach(notes, function(note) {\n note.asyncSave({ 'success': renderNotes });\n});\n// `renderNotes` is run once, after all notes have saved
\n\n\n\n\n
Creates a function that, when called, invokes func
with the this
binding of thisArg
and prepends any additional bind
arguments to those passed to the bound function.
- \n
func
(Function): The function to bind. \n[thisArg]
(Mixed): Thethis
binding offunc
. \n[arg1, arg2, ...]
(Mixed): Arguments to be partially applied. \n
(Function): Returns the new bound function.
\n\nvar func = function(greeting) {\n return greeting + ' ' + this.name;\n};\n\nfunc = _.bind(func, { 'name': 'moe' }, 'hi');\nfunc();\n// => 'hi moe'
\n\n\n\n\n
Binds methods on object
to object
, overwriting the existing method. Method names may be specified as individual arguments or as arrays of method names. If no method names are provided, all the function properties of object
will be bound.
- \n
object
(Object): The object to bind and assign the bound methods to. \n[methodName1, methodName2, ...]
(String): Method names on the object to bind. \n
(Object): Returns object
.
var view = {\n 'label': 'docs',\n 'onClick': function() { alert('clicked ' + this.label); }\n};\n\n_.bindAll(view);\njQuery('#docs').on('click', view.onClick);\n// => alerts 'clicked docs', when the button is clicked
\n\n\n\n\n
Creates a function that, when called, invokes the method at object[key]
and prepends any additional bindKey
arguments to those passed to the bound function. This method differs from _.bind
by allowing bound functions to reference methods that will be redefined or don't yet exist. See http://michaux.ca/articles/lazy-function-definition-pattern.
- \n
object
(Object): The object the method belongs to. \nkey
(String): The key of the method. \n[arg1, arg2, ...]
(Mixed): Arguments to be partially applied. \n
(Function): Returns the new bound function.
\n\nvar object = {\n 'name': 'moe',\n 'greet': function(greeting) {\n return greeting + ' ' + this.name;\n }\n};\n\nvar func = _.bindKey(object, 'greet', 'hi');\nfunc();\n// => 'hi moe'\n\nobject.greet = function(greeting) {\n return greeting + ', ' + this.name + '!';\n};\n\nfunc();\n// => 'hi, moe!'
\n\n\n\n\n
Creates a function that is the composition of the passed functions, where each function consumes the return value of the function that follows. For example, composing the functions f()
, g()
, and h()
produces f(g(h()))
. Each function is executed with the this
binding of the composed function.
- \n
[func1, func2, ...]
(Function): Functions to compose. \n
(Function): Returns the new composed function.
\n\nvar greet = function(name) { return 'hi ' + name; };\nvar exclaim = function(statement) { return statement + '!'; };\nvar welcome = _.compose(exclaim, greet);\nwelcome('moe');\n// => 'hi moe!'
\n\n\n\n\n
Creates a function that will delay the execution of func
until after wait
milliseconds have elapsed since the last time it was invoked. Pass true
for immediate
to cause debounce to invoke func
on the leading, instead of the trailing, edge of the wait
timeout. Subsequent calls to the debounced function will return the result of the last func
call.
- \n
func
(Function): The function to debounce. \nwait
(Number): The number of milliseconds to delay. \nimmediate
(Boolean): A flag to indicate execution is on the leading edge of the timeout. \n
(Function): Returns the new debounced function.
\n\nvar lazyLayout = _.debounce(calculateLayout, 300);\njQuery(window).on('resize', lazyLayout);
\n\n\n\n\n
Defers executing the func
function until the current call stack has cleared. Additional arguments will be passed to func
when it is invoked.
- \n
func
(Function): The function to defer. \n[arg1, arg2, ...]
(Mixed): Arguments to invoke the function with. \n
(Number): Returns the setTimeout
timeout id.
_.defer(function() { alert('deferred'); });\n// returns from the function before `alert` is called
\n\n\n\n\n
Executes the func
function after wait
milliseconds. Additional arguments will be passed to func
when it is invoked.
- \n
func
(Function): The function to delay. \nwait
(Number): The number of milliseconds to delay execution. \n[arg1, arg2, ...]
(Mixed): Arguments to invoke the function with. \n
(Number): Returns the setTimeout
timeout id.
var log = _.bind(console.log, console);\n_.delay(log, 1000, 'logged later');\n// => 'logged later' (Appears after one second.)
\n\n\n\n\n
Creates a 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. The func
is executed with the this
binding of the memoized function.
- \n
func
(Function): The function to have its output memoized. \n[resolver]
(Function): A function used to resolve the cache key. \n
(Function): Returns the new memoizing function.
\n\nvar fibonacci = _.memoize(function(n) {\n return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);\n});
\n\n\n\n\n
Creates a function that is restricted to execute func
once. Repeat calls to the function will return the value of the first call. The func
is executed with the this
binding of the created function.
- \n
func
(Function): The function to restrict. \n
(Function): Returns the new restricted function.
\n\nvar initialize = _.once(createApplication);\ninitialize();\ninitialize();\n// `initialize` executes `createApplication` once
\n\n\n\n\n
Creates a function that, when called, invokes func
with any additional partial
arguments prepended to those passed to the new function. This method is similar to _.bind
, except it does not alter the this
binding.
- \n
func
(Function): The function to partially apply arguments to. \n[arg1, arg2, ...]
(Mixed): Arguments to be partially applied. \n
(Function): Returns the new partially applied function.
\n\nvar greet = function(greeting, name) { return greeting + ' ' + name; };\nvar hi = _.partial(greet, 'hi');\nhi('moe');\n// => 'hi moe'
\n\n\n\n\n
This method is similar to _.partial
, except that partial
arguments are appended to those passed to the new function.
- \n
func
(Function): The function to partially apply arguments to. \n[arg1, arg2, ...]
(Mixed): Arguments to be partially applied. \n
(Function): Returns the new partially applied function.
\n\nvar defaultsDeep = _.partialRight(_.merge, _.defaults);\n\nvar options = {\n 'variable': 'data',\n 'imports': { 'jq': $ }\n};\n\ndefaultsDeep(options, _.templateSettings);\n\noptions.variable\n// => 'data'\n\noptions.imports\n// => { '_': _, 'jq': $ }
\n\n\n\n\n
Creates a function that, when executed, will only call the func
function at most once per every wait
milliseconds. If the throttled function is invoked more than once during the wait
timeout, func
will also be called on the trailing edge of the timeout. Subsequent calls to the throttled function will return the result of the last func
call.
- \n
func
(Function): The function to throttle. \nwait
(Number): The number of milliseconds to throttle executions to. \n
(Function): Returns the new throttled function.
\n\nvar throttled = _.throttle(updatePosition, 100);\njQuery(window).on('scroll', throttled);
\n\n\n\n\n
Creates a function that passes value
to the wrapper
function as its first argument. Additional arguments passed to the function are appended to those passed to the wrapper
function. The wrapper
is executed with the this
binding of the created function.
- \n
value
(Mixed): The value to wrap. \nwrapper
(Function): The wrapper function. \n
(Function): Returns the new function.
\n\nvar hello = function(name) { return 'hello ' + name; };\nhello = _.wrap(hello, function(func) {\n return 'before, ' + func('moe') + ', after';\n});\nhello();\n// => 'before, hello moe, after'
\n\n\n\n\n\n\n\n
Assigns own enumerable properties of source object(s) to the destination object. Subsequent sources will overwrite propery assignments of previous sources. If a callback
function is passed, it will be executed to produce the assigned values. The callback
is bound to thisArg
and invoked with two arguments; (objectValue, sourceValue).
extend
\n\n- \n
object
(Object): The destination object. \n[source1, source2, ...]
(Object): The source objects. \n[callback]
(Function): The function to customize assigning values. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns the destination object.
\n\n_.assign({ 'name': 'moe' }, { 'age': 40 });\n// => { 'name': 'moe', 'age': 40 }\n\nvar defaults = _.partialRight(_.assign, function(a, b) {\n return typeof a == 'undefined' ? b : a;\n});\n\nvar food = { 'name': 'apple' };\ndefaults(food, { 'name': 'banana', 'type': 'fruit' });\n// => { 'name': 'apple', 'type': 'fruit' }
\n\n\n\n\n
Creates a clone of value
. If deep
is true
, nested objects will also be cloned, otherwise they will be assigned by reference. If a callback
function is passed, it will be executed to produce the cloned values. If callback
returns undefined
, cloning will be handled by the method instead. The callback
is bound to thisArg
and invoked with one argument; (value).
- \n
value
(Mixed): The value to clone. \n[deep=false]
(Boolean): A flag to indicate a deep clone. \n[callback]
(Function): The function to customize cloning values. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the cloned value
.
var stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\nvar shallow = _.clone(stooges);\nshallow[0] === stooges[0];\n// => true\n\nvar deep = _.clone(stooges, true);\ndeep[0] === stooges[0];\n// => false\n\n_.mixin({\n 'clone': _.partialRight(_.clone, function(value) {\n return _.isElement(value) ? value.cloneNode(false) : undefined;\n })\n});\n\nvar clone = _.clone(document.body);\nclone.childNodes.length;\n// => 0
\n\n\n\n\n
Creates a deep clone of value
. If a callback
function is passed, it will be executed to produce the cloned values. If callback
returns the value it was passed, cloning will be handled by the method instead. The callback
is bound to thisArg
and invoked with one argument; (value).
Note: This function is loosely based on the structured clone algorithm. Functions and DOM nodes are not cloned. The enumerable properties of arguments
objects and objects created by constructors other than Object
are cloned to plain Object
objects. See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm.
- \n
value
(Mixed): The value to deep clone. \n[callback]
(Function): The function to customize cloning values. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Mixed): Returns the deep cloned value
.
var stooges = [\n { 'name': 'moe', 'age': 40 },\n { 'name': 'larry', 'age': 50 }\n];\n\nvar deep = _.cloneDeep(stooges);\ndeep[0] === stooges[0];\n// => false\n\nvar view = {\n 'label': 'docs',\n 'node': element\n};\n\nvar clone = _.cloneDeep(view, function(value) {\n return _.isElement(value) ? value.cloneNode(true) : value;\n});\n\nclone.node == view.node;\n// => false
\n\n\n\n\n
Assigns own enumerable properties of source object(s) to the destination object for all destination properties that resolve to undefined
. Once a property is set, additional defaults of the same property will be ignored.
- \n
object
(Object): The destination object. \n[source1, source2, ...]
(Object): The source objects. \n
(Object): Returns the destination object.
\n\nvar food = { 'name': 'apple' };\n_.defaults(food, { 'name': 'banana', 'type': 'fruit' });\n// => { 'name': 'apple', 'type': 'fruit' }
\n\n\n\n\n
Iterates over object
's own and inherited enumerable properties, executing the callback
for each property. The callback
is bound to thisArg
and invoked with three arguments; (value, key, object). Callbacks may exit iteration early by explicitly returning false
.
- \n
object
(Object): The object to iterate over. \n[callback=identity]
(Function): The function called per iteration. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns object
.
function Dog(name) {\n this.name = name;\n}\n\nDog.prototype.bark = function() {\n alert('Woof, woof!');\n};\n\n_.forIn(new Dog('Dagny'), function(value, key) {\n alert(key);\n});\n// => alerts 'name' and 'bark' (order is not guaranteed)
\n\n\n\n\n
Iterates over an object's own enumerable properties, executing the callback
for each property. The callback
is bound to thisArg
and invoked with three arguments; (value, key, object). Callbacks may exit iteration early by explicitly returning false
.
- \n
object
(Object): The object to iterate over. \n[callback=identity]
(Function): The function called per iteration. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns object
.
_.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {\n alert(key);\n});\n// => alerts '0', '1', and 'length' (order is not guaranteed)
\n\n\n\n\n
Creates a sorted array of all enumerable properties, own and inherited, of object
that have function values.
methods
\n\n- \n
object
(Object): The object to inspect. \n
(Array): Returns a new array of property names that have function values.
\n\n_.functions(_);\n// => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
\n\n\n\n\n
Checks if the specified object property
exists and is a direct property, instead of an inherited property.
- \n
object
(Object): The object to check. \nproperty
(String): The property to check for. \n
(Boolean): Returns true
if key is a direct property, else false
.
_.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');\n// => true
\n\n\n\n\n
Creates an object composed of the inverted keys and values of the given object
.
- \n
object
(Object): The object to invert. \n
(Object): Returns the created inverted object.
\n\n_.invert({ 'first': 'moe', 'second': 'larry' });\n// => { 'moe': 'first', 'larry': 'second' } (order is not guaranteed)
\n\n\n\n\n
Checks if value
is an arguments
object.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is an arguments
object, else false
.
(function() { return _.isArguments(arguments); })(1, 2, 3);\n// => true\n\n_.isArguments([1, 2, 3]);\n// => false
\n\n\n\n\n
Checks if value
is an array.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is an array, else false
.
(function() { return _.isArray(arguments); })();\n// => false\n\n_.isArray([1, 2, 3]);\n// => true
\n\n\n\n\n
Checks if value
is a boolean value.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a boolean value, else false
.
_.isBoolean(null);\n// => false
\n\n\n\n\n
Checks if value
is a date.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a date, else false
.
_.isDate(new Date);\n// => true
\n\n\n\n\n
Checks if value
is a DOM element.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a DOM element, else false
.
_.isElement(document.body);\n// => true
\n\n\n\n\n
Checks if value
is empty. Arrays, strings, or arguments
objects with a length of 0
and objects with no own enumerable properties are considered \"empty\".
- \n
value
(Array|Object|String): The value to inspect. \n
(Boolean): Returns true
, if the value
is empty, else false
.
_.isEmpty([1, 2, 3]);\n// => false\n\n_.isEmpty({});\n// => true\n\n_.isEmpty('');\n// => true
\n\n\n\n\n
Performs a deep comparison between two values to determine if they are equivalent to each other. If callback
is passed, it will be executed to compare values. If callback
returns undefined
, comparisons will be handled by the method instead. The callback
is bound to thisArg
and invoked with two arguments; (a, b).
- \n
a
(Mixed): The value to compare. \nb
(Mixed): The other value to compare. \n[callback]
(Function): The function to customize comparing values. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Boolean): Returns true
, if the values are equvalent, else false
.
var moe = { 'name': 'moe', 'age': 40 };\nvar copy = { 'name': 'moe', 'age': 40 };\n\nmoe == copy;\n// => false\n\n_.isEqual(moe, copy);\n// => true\n\nvar words = ['hello', 'goodbye'];\nvar otherWords = ['hi', 'goodbye'];\n\n_.isEqual(words, otherWords, function(a, b) {\n var reGreet = /^(?:hello|hi)$/i,\n aGreet = _.isString(a) && reGreet.test(a),\n bGreet = _.isString(b) && reGreet.test(b);\n\n return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;\n});\n// => true
\n\n\n\n\n
Checks if value
is, or can be coerced to, a finite number.
Note: This is not the same as native isFinite
, which will return true for booleans and empty strings. See http://es5.github.com/#x15.1.2.5.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is finite, else false
.
_.isFinite(-101);\n// => true\n\n_.isFinite('10');\n// => true\n\n_.isFinite(true);\n// => false\n\n_.isFinite('');\n// => false\n\n_.isFinite(Infinity);\n// => false
\n\n\n\n\n
Checks if value
is a function.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a function, else false
.
_.isFunction(_);\n// => true
\n\n\n\n\n
Checks if 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.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is NaN
, else false
.
_.isNaN(NaN);\n// => true\n\n_.isNaN(new Number(NaN));\n// => true\n\nisNaN(undefined);\n// => true\n\n_.isNaN(undefined);\n// => false
\n\n\n\n\n
Checks if value
is null
.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is null
, else false
.
_.isNull(null);\n// => true\n\n_.isNull(undefined);\n// => false
\n\n\n\n\n
Checks if value
is a number.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a number, else false
.
_.isNumber(8.4 * 5);\n// => true
\n\n\n\n\n
Checks if value
is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0)
, and new String('')
)
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is an object, else false
.
_.isObject({});\n// => true\n\n_.isObject([1, 2, 3]);\n// => true\n\n_.isObject(1);\n// => false
\n\n\n\n\n
Checks if a given value
is an object created by the Object
constructor.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if value
is a plain object, else false
.
function Stooge(name, age) {\n this.name = name;\n this.age = age;\n}\n\n_.isPlainObject(new Stooge('moe', 40));\n// => false\n\n_.isPlainObject([1, 2, 3]);\n// => false\n\n_.isPlainObject({ 'name': 'moe', 'age': 40 });\n// => true
\n\n\n\n\n
Checks if value
is a regular expression.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a regular expression, else false
.
_.isRegExp(/moe/);\n// => true
\n\n\n\n\n
Checks if value
is a string.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is a string, else false
.
_.isString('moe');\n// => true
\n\n\n\n\n
Checks if value
is undefined
.
- \n
value
(Mixed): The value to check. \n
(Boolean): Returns true
, if the value
is undefined
, else false
.
_.isUndefined(void 0);\n// => true
\n\n\n\n\n
Creates an array composed of the own enumerable property names of object
.
- \n
object
(Object): The object to inspect. \n
(Array): Returns a new array of property names.
\n\n_.keys({ 'one': 1, 'two': 2, 'three': 3 });\n// => ['one', 'two', 'three'] (order is not guaranteed)
\n\n\n\n\n
Recursively merges own enumerable properties of the source object(s), that don't resolve to undefined
, into the destination object. Subsequent sources will overwrite propery assignments of previous sources. If a callback
function is passed, it will be executed to produce the merged values of the destination and source properties. If callback
returns undefined
, merging will be handled by the method instead. The callback
is bound to thisArg
and invoked with two arguments; (objectValue, sourceValue).
- \n
object
(Object): The destination object. \n[source1, source2, ...]
(Object): The source objects. \n[callback]
(Function): The function to customize merging properties. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns the destination object.
\n\nvar names = {\n 'stooges': [\n { 'name': 'moe' },\n { 'name': 'larry' }\n ]\n};\n\nvar ages = {\n 'stooges': [\n { 'age': 40 },\n { 'age': 50 }\n ]\n};\n\n_.merge(names, ages);\n// => { 'stooges': [{ 'name': 'moe', 'age': 40 }, { 'name': 'larry', 'age': 50 }] }\n\nvar food = {\n 'fruits': ['apple'],\n 'vegetables': ['beet']\n};\n\nvar otherFood = {\n 'fruits': ['banana'],\n 'vegetables': ['carrot']\n};\n\n_.merge(food, otherFood, function(a, b) {\n return _.isArray(a) ? a.concat(b) : undefined;\n});\n// => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }
\n\n\n\n\n
Creates a shallow clone of object
excluding the specified properties. Property names may be specified as individual arguments or as arrays of property names. If a callback
function is passed, it will be executed for each property in the object
, omitting the properties callback
returns truthy for. The callback
is bound to thisArg
and invoked with three arguments; (value, key, object).
- \n
object
(Object): The source object. \ncallback|[prop1, prop2, ...]
(Function|String): The properties to omit or the function called per iteration. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns an object without the omitted properties.
\n\n_.omit({ 'name': 'moe', 'age': 40 }, 'age');\n// => { 'name': 'moe' }\n\n_.omit({ 'name': 'moe', 'age': 40 }, function(value) {\n return typeof value == 'number';\n});\n// => { 'name': 'moe' }
\n\n\n\n\n
Creates a two dimensional array of the given object's key-value pairs, i.e. [[key1, value1], [key2, value2]]
.
- \n
object
(Object): The object to inspect. \n
(Array): Returns new array of key-value pairs.
\n\n_.pairs({ 'moe': 30, 'larry': 40 });\n// => [['moe', 30], ['larry', 40]] (order is not guaranteed)
\n\n\n\n\n
Creates a shallow clone of object
composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. If callback
is passed, it will be executed for each property in the object
, picking the properties callback
returns truthy for. The callback
is bound to thisArg
and invoked with three arguments; (value, key, object).
- \n
object
(Object): The source object. \ncallback|[prop1, prop2, ...]
(Array|Function|String): The function called per iteration or properties to pick, either as individual arguments or arrays. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Object): Returns an object composed of the picked properties.
\n\n_.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name');\n// => { 'name': 'moe' }\n\n_.pick({ 'name': 'moe', '_userid': 'moe1' }, function(value, key) {\n return key.charAt(0) != '_';\n});\n// => { 'name': 'moe' }
\n\n\n\n\n
Creates an array composed of the own enumerable property values of object
.
- \n
object
(Object): The object to inspect. \n
(Array): Returns a new array of property values.
\n\n_.values({ 'one': 1, 'two': 2, 'three': 3 });\n// => [1, 2, 3]
\n\n\n\n\n\n\n\n
Converts the characters &
, <
, >
, \"
, and '
in string
to their corresponding HTML entities.
- \n
string
(String): The string to escape. \n
(String): Returns the escaped string.
\n\n_.escape('Moe, Larry & Curly');\n// => 'Moe, Larry & Curly'
\n\n\n\n\n
This function returns the first argument passed to it.
\n\n- \n
value
(Mixed): Any value. \n
(Mixed): Returns value
.
var moe = { 'name': 'moe' };\nmoe === _.identity(moe);\n// => true
\n\n\n\n\n
Adds functions properties of object
to the lodash
function and chainable wrapper.
- \n
object
(Object): The object of function properties to add tolodash
. \n
_.mixin({\n 'capitalize': function(string) {\n return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();\n }\n});\n\n_.capitalize('moe');\n// => 'Moe'\n\n_('moe').capitalize();\n// => 'Moe'
\n\n\n\n\n
Reverts the '_' variable to its previous value and returns a reference to the lodash
function.
(Function): Returns the lodash
function.
var lodash = _.noConflict();
\n\n\n\n\n
Produces a random number between min
and max
(inclusive). If only one argument is passed, a number between 0
and the given number will be returned.
- \n
[min=0]
(Number): The minimum possible value. \n[max=1]
(Number): The maximum possible value. \n
(Number): Returns a random number.
\n\n_.random(0, 5);\n// => a number between 0 and 5\n\n_.random(5);\n// => also a number between 0 and 5
\n\n\n\n\n
Resolves the value of property
on object
. If property
is a function, it will be invoked and its result returned, else the property value is returned. If object
is falsey, then null
is returned.
- \n
object
(Object): The object to inspect. \nproperty
(String): The property to get the value of. \n
(Mixed): Returns the resolved value.
\n\nvar object = {\n 'cheese': 'crumpets',\n 'stuff': function() {\n return 'nonsense';\n }\n};\n\n_.result(object, 'cheese');\n// => 'crumpets'\n\n_.result(object, 'stuff');\n// => 'nonsense'
\n\n\n\n\n
A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code.
\nNote: In the development build, _.template
utilizes sourceURLs for easier debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
Note: Lo-Dash may be used in Chrome extensions by either creating a lodash csp
build and using precompiled templates, or loading Lo-Dash in a sandbox.
For more information on precompiling templates see:
\nhttp://lodash.com/#custom-builds
For more information on Chrome extension sandboxes see:
\nhttp://developer.chrome.com/stable/extensions/sandboxingEval.html
- \n
text
(String): The template text. \ndata
(Obect): The data object used to populate the text. \noptions
(Object): The options object. escape - The \"escape\" delimiter regexp. evaluate - The \"evaluate\" delimiter regexp. interpolate - The \"interpolate\" delimiter regexp. sourceURL - The sourceURL of the template's compiled source. variable - The data object variable name. \n
(Function, String): Returns a compiled function when no data
object is given, else it returns the interpolated text.
// using a compiled template\nvar compiled = _.template('hello <%= name %>');\ncompiled({ 'name': 'moe' });\n// => 'hello moe'\n\nvar list = '<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>';\n_.template(list, { 'people': ['moe', 'larry'] });\n// => '<li>moe</li><li>larry</li>'\n\n// using the \"escape\" delimiter to escape HTML in data property values\n_.template('<b><%- value %></b>', { 'value': '<script>' });\n// => '<b><script></b>'\n\n// using the ES6 delimiter as an alternative to the default \"interpolate\" delimiter\n_.template('hello ${ name }', { 'name': 'curly' });\n// => 'hello curly'\n\n// using the internal `print` function in \"evaluate\" delimiters\n_.template('<% print(\"hello \" + epithet); %>!', { 'epithet': 'stooge' });\n// => 'hello stooge!'\n\n// using custom template delimiters\n_.templateSettings = {\n 'interpolate': /{{([\\s\\S]+?)}}/g\n};\n\n_.template('hello {{ name }}!', { 'name': 'mustache' });\n// => 'hello mustache!'\n\n// using the `sourceURL` option to specify a custom sourceURL for the template\nvar compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });\ncompiled(data);\n// => find the source of \"greeting.jst\" under the Sources tab or Resources panel of the web inspector\n\n// using the `variable` option to ensure a with-statement isn't used in the compiled template\nvar compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });\ncompiled.source;\n// => function(data) {\n var __t, __p = '', __e = _.escape;\n __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';\n return __p;\n}\n\n// using the `source` property to inline compiled templates for meaningful\n// line numbers in error messages and a stack trace\nfs.writeFileSync(path.join(cwd, 'jst.js'), '\\\n var JST = {\\\n \"main\": ' + _.template(mainText).source + '\\\n };\\\n');
\n\n\n\n\n
Executes the callback
function n
times, returning an array of the results of each callback
execution. The callback
is bound to thisArg
and invoked with one argument; (index).
- \n
n
(Number): The number of times to execute the callback. \ncallback
(Function): The function called per iteration. \n[thisArg]
(Mixed): Thethis
binding ofcallback
. \n
(Array): Returns a new array of the results of each callback
execution.
var diceRolls = _.times(3, _.partial(_.random, 1, 6));\n// => [3, 6, 4]\n\n_.times(3, function(n) { mage.castSpell(n); });\n// => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively\n\n_.times(3, function(n) { this.cast(n); }, mage);\n// => also calls `mage.castSpell(n)` three times
\n\n\n\n\n
The opposite of _.escape
, this method converts the HTML entities &
, <
, >
, "
, and '
in string
to their corresponding characters.
- \n
string
(String): The string to unescape. \n
(String): Returns the unescaped string.
\n\n_.unescape('Moe, Larry & Curly');\n// => 'Moe, Larry & Curly'
\n\n\n\n\n
Generates a unique ID. If prefix
is passed, the ID will be appended to it.
- \n
[prefix]
(String): The value to prefix the ID with. \n
(String): Returns the unique ID.
\n\n_.uniqueId('contact_');\n// => 'contact_104'\n\n_.uniqueId();\n// => '105'
\n\n\n\n\n\n\n\n
A reference to the lodash
function.
\n\n\n\n\n\n\n\n
(String): The semantic version number.
\n\n\n\n\n\n
(Object): By default, the template delimiters used by Lo-Dash are similar to those in embedded Ruby (ERB). Change the following template settings to use alternative delimiters.
\n\n\n\n\n\n
(RegExp): Used to detect data
property values to be HTML-escaped.
\n\n\n\n\n
(RegExp): Used to detect code to be evaluated.
\n\n\n\n\n\n
(RegExp): Used to detect data
property values to inject.
\n\n\n\n\n
(String): Used to reference the data object in the template text.
\n\n\n\n\n\n
(Object): Used to import variables into the compiled template.
\n\n\n\n\n