lodash common APIs
Chinese documents https://www.lodashjs.com/
Array: applicable to array types, such as filling data, finding elements, array fragmentation, etc
Collection: applicable to array and object types, and partially applicable to string operations, such as grouping, finding, filtering, etc
Function: applicable to function types, such as throttling, delaying, caching, setting hooks, etc
Lang: generally applicable to various types. It is often used to perform type judgment and type conversion
Math: applicable to numeric types, often used to perform mathematical operations
Number: used to generate random numbers and compare the relationship between values and value ranges
Object: applicable to object types. It is commonly used for object creation, extension, type conversion, retrieval, collection, and other operations
Seq: commonly used to create chained calls to improve execution performance (lazy Computing)
String: applicable to string type
Usage
npm i -save lodash \\Global installation
Some methods are encapsulated in the utils toolkit, and then mounted to the global. Because the global cost of mounting is very large, they are commonly used in some projects. The following methods are introduced as needed:
import _get from 'lodash/get' import _map from 'lodash/map' import _uniq from 'lodash/uniq' import _pick from 'lodash/pick' import _omit from 'lodash/omit' import _isNaN from 'lodash/isNaN' import _property from 'lodash/property' import _findIndex from 'lodash/findIndex' import _isUndefined from 'lodash/isUndefined' import _isString from 'lodash/isString' import _debounce from 'lodash/debounce'
array
Array operation
_. chunk(array, [size=1]) array splitting
Split the array into multiple size length blocks, and make these blocks into a new array. If the array cannot be divided into all equal length blocks, the last remaining elements will form a block.
Import version
3.0.0
parameter
- array (Array): array to be processed
- [size=1] (number): length of each array block
return
(Array): returns a new array containing split blocks (Note: equivalent to a two-dimensional Array).
example
_.chunk(['a', 'b', 'c', 'd'], 2); // => [['a', 'b'], ['c', 'd']] _.chunk(['a', 'b', 'c', 'd'], 3); // => [['a', 'b', 'c'], ['d']]
_. compact(array) array to remove false values
Create a new array containing all the non dummy elements in the original array. For example, false, null,0, "", undefined, and NaN are all considered false values.
Import version
0.1.0
parameter
- array (Array): array to be processed
Return value
(Array): returns a new array that filters out false values.
example
_.compact([0, 1, false, 2, '', 3]); // => [1, 2, 3]
_. concat(array, [values]) most groups (values) are linked into an array
Create a new array and concatenate the array with any array or value.
Import version
4.0.0
parameter
- array (Array): connected array.
- [values] (... *): the value of the connection.
Return value
(Array): returns the new array after connection.
example
var array = [1]; var other = _.concat(array, 2, [3], [[4]]); console.log(other); // => [1, 2, 3, [4]] console.log(array); // => [1]
_.join(array, [separator=',']) Array to string
Converts all elements in the array to a separator delimited string.
Import version
4.0.0
parameter
- array (Array): the array to convert.
- [separator=','] (string): separates elements.
Return value
(string): returns the connection string.
example
_.join(['a', 'b', 'c'], '~'); // => 'a~b~c'
_.remove(array, [predicate=_.identity]) Remove all elements with true values from the array
Remove all elements in the array whose predicate (assertion) returns true value, and return the array composed of removed elements. The predicate (assertion) will pass in three parameters: (value, index, array).
Note: and _.filter Different, this method will change the array. use _.pull To remove elements from the array based on the supplied value.
Add version
2.0.0
parameter
- array (Array): the array to modify.
- [predicate=|. Identity] (array|function|object|string): the function called for each iteration.
return
(Array): returns a new array composed of removed elements.
example
var array = [1, 2, 3, 4]; var evens = _.remove(array, function(n) { return n % 2 == 0; }); console.log(array); // => [1, 3] console.log(evens); // => [2, 4]
_.reverse(array) Invert array
Invert the array so that the first element becomes the last element, the second element becomes the penultimate element, and so on.
Note: this method will change the original array, based on Array#reverse.
Add version
4.0.0
parameter
- array (Array): the array to modify.
return
(Array): returns array
example
var array = [1, 2, 3]; _.reverse(array); // => [3, 2, 1] console.log(array); // => [3, 2, 1]
_.unzip(array)
This method is similar to _.zip , except that it receives an array of grouped elements and creates an array that groups elements into the structure before packaging. (: the first element of the returned array contains the first element of all input arrays, the first element contains the second element of all input arrays, and so on.)
Add version
1.2.0
parameter
- array (Array): an array of grouped elements to process.
return
(Array): returns a new array of reorganized elements.
example
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]); // => [['fred', 30, true], ['barney', 40, false]] _.unzip(zipped); // => [['fred', 'barney'], [30, 40], [true, false]]
_.unzipWith(array, [iteratee=_.identity])
This method is similar to _.unzip Except that it accepts an iterate specifying how the restructured values should be combined. When iteratee is called, the value of each group will be passed in: (... group).
Add version
3.8.0
parameter
- array (Array): an array of grouped elements to process.
- [iteratee=\identity] (function): this function is used to combine the reorganized values.
return
(Array): returns a new array of reorganized elements.
example
var zipped = _.zip([1, 2], [10, 20], [100, 200]); // => [[1, 10, 100], [2, 20, 200]] _.unzipWith(zipped, _.add); // => [3, 30, 300]
_.without(array, [values])
Create a new array that eliminates all given values. When removing values, use SameValueZero Make an equality comparison.
Note: no _.pull , this method will return a new array.
Add version
0.1.0
parameter
- array (Array): the array to check.
- [values] (... *): the value to be culled.
return
(Array): returns a new array of filtered values.
example
_.without([2, 1, 2, 3], 1, 2); // => [3]
_.xor([arrays])
To create an array of unique values for a given array, use symmetric difference Make equivalence comparison. The order of the returned values depends on the order in which they appear in the array.
Add version
2.4.0
parameter
- [arrays] (... Array): array to check.
return
(Array): returns a new array of filtered values.
example
_.xor([2, 1], [2, 3]); // => [1, 3]
_.xorBy([arrays], [iteratee=_.identity])
This method is similar _.xor , except that it accepts iteratee (iterator), which calls each value of each array to generate a new value for comparison. Iteratee calls a parameter: (value)
Add version
4.0.0
parameter
- [arrays] (... Array): array to check.
- [iteratee=|. Identity] (array|function|object|string): call the iterative function of each element.
return
(Array): returns a new array of filtered values.
example
_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor); // => [1.2, 3.4] // The `_.property` iteratee shorthand. _.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 2 }]
_.xorWith([arrays], [comparator])
The method is like _.xor , except that it accepts a comparator to call the elements of the comparison array. The comparator calls two parameters: (arrVal, othVal)
Add version
4.0.0
parameter
- [arrays] (... Array): array to check.
- [comparator] (Function): call the comparison function of each element.
return
(Array): returns a new array of filtered values.
example
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.xorWith(objects, others, _.isEqual); // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
Combine multiple arrays into specific types
_.zip([arrays])
Create an array of grouped elements. The first element of the array contains the first element of all given arrays, the second element of the array contains the second element of all given arrays, and so on.
Add version
0.1.0
parameter
- [arrays] (... Array): array to process.
return
(Array): returns a new array of grouped elements.
example
_.zip(['fred', 'barney'], [30, 40], [true, false]); // => [['fred', 30, true], ['barney', 40, false]]
_.zipObject([props=[]], [values=[]])
This method is similar _.fromPairs , except that it accepts 2 arrays. The value in the first array is used as the attribute identifier (attribute name), and the value in the second array is used as the corresponding attribute value.
Add version
0.4.0
parameter
- [props=[]] (Array): The property identifiers.
- [values=[]] (Array): The property values.
return
(Object): Returns the new object.
example
_.zipObject(['a', 'b'], [1, 2]); // => { 'a': 1, 'b': 2 }
_.zipObjectDeep([props=[]], [values=[]])
This method is similar _.zipObject , except that it supports attribute paths.
Add version
4.1.0
parameter
- [props=[]] (Array): attribute identifier (attribute name).
- [values=[]] (Array): attribute value.
return
(Object): returns a new object.
example
_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]); // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
_.zipWith([arrays], [iteratee=_.identity])
This method is similar to _.zip , the difference is that it accepts an iteratee (iteration function) to specify how the grouped values should be combined. The iteratee calls the elements of each group: (... group)
Add version
3.8.0
parameter
- [arrays] (... Array): array to process.
- [iteratee=\identity] (function): the function is used to combine grouped values.
return
(Array): returns a new array of grouped elements.
example
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) { return a + b + c; }); // => [111, 222]
Exclude given value
_.difference(array, [values])
Creates an array with unique array values, each of which is not contained in any other given array. (Note: create a new array. The values in this array exclude the values in the given array for the first number (array parameter) This method uses SameValueZero Make an equality comparison. The order of the resulting values is determined by the order in the first array.
Note: no _.pullAll , this method will return a new array.
Import version
0.1.0
parameter
- array (Array): the array to check.
- [values] (... Array): excluded values.
Return value
(Array): returns a new array of filtered values.
example
_.difference([3, 2, 1], [4, 2]); // => [3, 1]
_.differenceBy(array, [values], [iteratee=_.identity])
This method is similar _.difference , except that it accepts an iteratee (Note: iterator) and calls each element in array and values to produce a comparison criterion. The resulting value is selected from the first array. Iteratee will call a parameter: (value). (Note: first, use the iterator to iterate each element in array and values respectively, and the returned value is used as the comparison value).
Note: no _.pullAllBy , this method will return a new array.
Import version
4.0.0
parameter
- array (Array): the array to check.
- [values] (... Array): excluded values.
- [iteratee=|. Identity] (array|function|object|string): iteratee calls each element.
Return value
(Array): returns a new array of filtered values.
example
_.differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor); // => [3.1, 1.3] // The `_.property` iteratee shorthand. _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x'); // => [{ 'x': 2 }]
_.differenceWith(array, [values], [comparator])
This method is similar _.difference , except that it accepts a comparator (Note: comparator), which calls to compare the elements in array, values. The resulting value is selected from the first array. There are two parameters for the comparator call: (arrVal, othVal).
Note: no _.pullAllWith , this method will return a new array.
Import version
4.0.0
parameter
- array (Array): the array to check.
- [values] (... Array): excluded values.
- [comparator] (function): the comparator calls each element.
Return value
(Array): returns a new array of filtered values.
example
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual); // => [{ 'x': 2, 'y': 1 }]
Array slice
_.drop(array, [n=1])
Create a slice array and remove the n elements in front of the array. (n the default value is 1.)
Import version
0.5.0
parameter
- array (Array): the array to query.
- [n=1] (number): number of elements to be removed.
Return value
(Array): returns the remaining slices of array.
example
_.drop([1, 2, 3]); // => [2, 3] _.drop([1, 2, 3], 2); // => [3] _.drop([1, 2, 3], 5); // => [] _.drop([1, 2, 3], 0); // => [1, 2, 3]
_.dropRight(array, [n=1])
Create a slice array and remove the n elements at the end of the array. (n the default value is 1.)
Import version
3.0.0
parameter
- array (Array): the array to query.
- [n=1] (number): number of elements to be removed.
Return value
(Array): returns the remaining slices of array.
example
_.dropRight([1, 2, 3]); // => [1, 2] _.dropRight([1, 2, 3], 2); // => [1] _.dropRight([1, 2, 3], 5); // => [] _.dropRight([1, 2, 3], 0); // => [1, 2, 3]
_.dropRightWhile(array, [predicate=_.identity])
Create a slice array and remove the part from the beginning to the end of the array from the false value returned by predicate. Predict will pass in three parameters: (value, index, array).
Import version
3.0.0
parameter
- array (Array): the array to query.
- [predicate=\identity] (function): this function will be called in each iteration.
Return value
(Array): returns the remaining slices of array.
example
var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false } ]; _.dropRightWhile(users, function(o) { return !o.active; }); // => objects for ['barney'] // The `_.matches` iteratee shorthand. _.dropRightWhile(users, { 'user': 'pebbles', 'active': false }); // => objects for ['barney', 'fred'] // The `_.matchesProperty` iteratee shorthand. _.dropRightWhile(users, ['active', false]); // => objects for ['barney'] // The `_.property` iteratee shorthand. _.dropRightWhile(users, 'active'); // => objects for ['barney', 'fred', 'pebbles']
_.dropWhile(array, [predicate=_.identity])
Create a slice array and remove the part in the array from the starting point to the end of the false value returned by predicate. Predict will pass in three parameters: (value, index, array).
Import version
3.0.0
parameter
- array (Array): the array to query.
- [predicate=\identity] (function): this function will be called in each iteration.
Return value
(Array): returns the remaining slices of array.
example
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true } ]; _.dropWhile(users, function(o) { return !o.active; }); // => objects for ['pebbles'] // The `_.matches` iteratee shorthand. _.dropWhile(users, { 'user': 'barney', 'active': false }); // => objects for ['fred', 'pebbles'] // The `_.matchesProperty` iteratee shorthand. _.dropWhile(users, ['active', false]); // => objects for ['pebbles'] // The `_.property` iteratee shorthand. _.dropWhile(users, 'active'); // => objects for ['barney', 'fred', 'pebbles']
_.slice(array, [start=0], [end=array.length])
The array array is cropped from the start position to the end position, but the end position is not included.
Note: this method is used to replace Array#slice To ensure that the array returns correctly.
Add version
3.0.0
parameter
- array (Array): to crop the array.
- [start=0] (number): start position.
- [end=array.length] (number): end position.
return
(Array): returns a new array of the cropped part of array array.
_.take(array, [n=1])
Create an array slice and extract n elements from the starting element of the array.
Add version
0.1.0
parameter
- array (Array): the array to retrieve.
- [n=1] (number): the number of elements to extract.
return
(Array): returns the slice of the array (n elements starting from the starting element).
example
_.take([1, 2, 3]); // => [1] _.take([1, 2, 3], 2); // => [1, 2] _.take([1, 2, 3], 5); // => [1, 2, 3] _.take([1, 2, 3], 0); // => []
_.takeRight(array, [n=1])
Create an array slice and extract n elements from the last element of the array.
Add version
3.0.0
parameter
- array (Array): the array to retrieve.
- [n=1] (number): the number of elements to extract.
return
(Array): returns the slice of the array (n elements from the end element).
example
_.takeRight([1, 2, 3]); // => [3] _.takeRight([1, 2, 3], 2); // => [2, 3] _.takeRight([1, 2, 3], 5); // => [1, 2, 3] _.takeRight([1, 2, 3], 0); // => []
_.takeRightWhile(array, [predicate=_.identity])
The element is extracted from the last element of the array until the predicate returns a false value. Predict will pass in three parameters: (value, index, array).
Add version
3.0.0
parameter
- array (Array): the array to retrieve.
- [predicate=|. Identity] (array|function|object|string): the function called for each iteration.
return
(Array): returns the slice of the array.
example
var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false } ]; _.takeRightWhile(users, function(o) { return !o.active; }); // => objects for ['fred', 'pebbles'] // The `_.matches` iteratee shorthand. _.takeRightWhile(users, { 'user': 'pebbles', 'active': false }); // => objects for ['pebbles'] // The `_.matchesProperty` iteratee shorthand. _.takeRightWhile(users, ['active', false]); // => objects for ['fred', 'pebbles'] // The `_.property` iteratee shorthand. _.takeRightWhile(users, 'active'); // => []
_.takeWhile(array, [predicate=_.identity])
The element is extracted from the starting element of the array until the predicate returns a false value. Predict will pass in three parameters: (value, index, array).
Add version
3.0.0
parameter
- array (Array): array to be processed
- [predicate=|. Identity] (array|function|object|string): the function called for each iteration.
return
(Array): returns the slice of the array.
example
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false}, { 'user': 'pebbles', 'active': true } ]; _.takeWhile(users, function(o) { return !o.active; }); // => objects for ['barney', 'fred'] // The `_.matches` iteratee shorthand. _.takeWhile(users, { 'user': 'barney', 'active': false }); // => objects for ['barney'] // The `_.matchesProperty` iteratee shorthand. _.takeWhile(users, ['active', false]); // => objects for ['barney', 'fred'] // The `_.property` iteratee shorthand. _.takeWhile(users, 'active'); // => []
Find and replace array values (indexes)
_.fill(array, value, [start=0], [end=array.length]) Value substitution
Fill (replace) the array with the value value, starting at the start position and ending at the end position (but excluding the end position).
Note: this method will change the array (Note: it does not create a new array).
Import version
3.2.0
parameter
- array (Array): the array to be filled with changes.
- Value (*): the value filled into the array.
- [start=0] (number): start position (default 0).
- [end=array.length] (number): end position (default array.length).
Return value
(Array): returns array.
example
var array = [1, 2, 3]; _.fill(array, 'a'); console.log(array); // => ['a', 'a', 'a'] _.fill(Array(3), 2); // => [2, 2, 2] _.fill([4, 6, 8, 10], '*', 1, 3); // => [4, '*', '*', 10]
_.findIndex(array, [predicate=_.identity], [fromIndex=0]) Return array element index value
The method is similar _.find The difference is that the method returns the index value of the first element that is judged to be true by predicate, rather than the element itself.
Import version
1.1.0
parameter
- array (Array): the array to search.
- [predicate=|. Identity] (array|function|object|string): this function will be called in each iteration.
- [fromIndex=0] (number): The index to search from.
Return value
(number): returns the index value of the found element. Otherwise, -1 is returned.
example
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true } ]; _.findIndex(users, function(o) { return o.user == 'barney'; }); // => 0 // The `_.matches` iteratee shorthand. _.findIndex(users, { 'user': 'fred', 'active': false }); // => 1 // The `_.matchesProperty` iteratee shorthand. _.findIndex(users, ['active', false]); // => 0 // The `_.property` iteratee shorthand. _.findIndex(users, 'active'); // => 2
_.head(array) Returns the first element of an array
Gets the first element of the array array.
Import version
0.1.0
alias
_.first
parameter
- array (Array): the array to query.
Return value
(*): returns the first element of the array array.
example
_.head([1, 2, 3]); // => 1 _.head([]); // => undefined
_.last(array) Returns the last element
Gets the last element in the array.
Import version
0.1.0
parameter
-
array (Array): the array to retrieve.
-
Return value
(*): returns the last element in the array
example
_.last([1, 2, 3]); // => 3
_.indexOf(array, value, [fromIndex=0]) Returns the index in an array
use SameValueZero Equivalent comparison: returns the index value of the first value found in the array. If fromIndex is a negative value, matching will be performed from the index at the end of the array.
Import version
0.1.0
parameter
- array (Array): the array to find.
- Value (*): the value to be searched.
- [fromIndex=0] (number): the location to start the query.
Return value
(number): returns the index position of the value in the array. If it is not found, it returns -1.
example
_.indexOf([1, 2, 1, 2], 2); // => 1 // Search from the `fromIndex`. _.indexOf([1, 2, 1, 2], 2, 2); // => 3
_.lastIndexOf(array, value, [fromIndex=array.length-1]) Returns the index value of the matching value
This method is similar _.indexOf , the difference is that it traverses the elements of the array from right to left.
Import version
0.1.0
parameter
- array (Array): the array to search.
- Value (*): the value to search.
- [fromIndex=array.length-1] (number): the index value to start the search.
Return value
(number): returns the index value of the matching value, otherwise -1.
example
_.lastIndexOf([1, 2, 1, 2], 2); // => 3 // Search from the `fromIndex`. _.lastIndexOf([1, 2, 1, 2], 2, 2); // => 1
_.initial(array) Remove the last element in the array array
Get all elements except the last element in the array (Note: remove the last element in the array).
Import version
0.1.0
parameter
- array (Array): the array to query.
Return value
(Array): returns the intercepted array array.
example
_.initial([1, 2, 3]); // => [1, 2]
_.tail(array) Remove the first element in the array array
Gets all elements except the first element of the array.
Add version
4.0.0
parameter
- array (Array): the array to retrieve.
return
(Array): returns the slice of the array (all elements except the first element of the Array).
example
_.tail([1, 2, 3]); // => [2, 3]
Array intersection
_.intersection([arrays]) Intersection of a given array
Create an array of unique values that contains all the elements contained in a given array, using SameValueZero Perform equality comparison. (Note: it can be understood as the intersection of a given array)
Import version
0.1.0
parameter
- [arrays] (... Array): array to be checked.
Return value
(Array): returns a new array containing all the intersection elements of the passed in array.
example
_.intersection([2, 1], [4, 2], [1, 2]); // => [2]
_.intersectionBy([arrays], [iteratee=_.identity])
This method is similar _.intersection , the difference is that it accepts an iteratee to call each value of each arrays to generate a value, and compares the generated values. The resulting value is selected from the first array. Iteratee will pass in a parameter: (value).
Import version
4.0.0
parameter
- [arrays] (... Array): array to be checked.
- [iteratee=|. Identity] (array|function|object|string): iteratee (iterator) calls each element.
Return value
(Array): returns a new array containing all the intersection elements of the passed in array.
example
_.intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor); // => [2.1] // The `_.property` iteratee shorthand. _.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }]
_.intersectionWith([arrays], [comparator])
This method is similar _.intersection The difference is that it accepts a comparator call to compare elements in arrays. The resulting value is selected from the first array. The comparator will pass in two parameters: (arrVal, othVal).
Import version
4.0.0
parameter
- [arrays] (... Array): array to be checked.
- [comparator] (function): the comparator calls each element.
Return value
(Array): returns a new array containing all the intersection elements of the passed in array.
example
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.intersectionWith(objects, others, _.isEqual); // => [{ 'x': 1, 'y': 2 }]
_.union([arrays])
Creates an array of unique values in order. All element values of the given array use SameValueZero Make equivalence comparison. (Note: the union of arrays is returned in order, and the elements of the returned array are unique)
Add version
0.1.0
parameter
- [arrays] (... Array): array to check.
return
(Array): returns a new associative array.
example
_.union([2], [1, 2]); // => [2, 1]
_.unionBy([arrays], [iteratee=_.identity])
This method is similar _.union , except that it accepts an iteratee (iterative function) and calls each element of each array to generate the criteria for uniqueness calculation. Iteratee will pass in a parameter: (value).
Add version
4.0.0
parameter
- [arrays] (... Array): array to check.
- [iteratee=|. Identity] (array|function|object|string): iterates the function and calls each element.
return
(Array): returns a new associative array.
example
_.unionBy([2.1], [1.2, 2.3], Math.floor); // => [2.1, 1.2] // The `_.property` iteratee shorthand. _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }, { 'x': 2 }]
_.unionWith([arrays], [comparator])
This method is similar _.union , except that it accepts a comparator call to compare each element of the arrays array. When calling the comparator, two parameters will be passed in: (arrVal, othVal).
Add version
4.0.0
parameter
- [arrays] (... Array): array to check.
- [comparator] (Function): compare functions and call each element.
return
(Array): returns a new associative array.
example
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]; var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.unionWith(objects, others, _.isEqual); // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
Remove all elements in the array with equal given values
_.pull(array, [values])
Remove all elements in the array that are equal to the given value, and use SameValueZero Perform a congruent comparison.
Note: and _.without Method, which changes the array. use _.remove Removes elements from an array.
Import version
2.0.0
parameter
- array (Array): the array to modify.
- [values] (... *): the value to delete.
Return value
(Array): returns array
example
var array = [1, 2, 3, 1, 2, 3]; _.pull(array, 2, 3); console.log(array); // => [1, 1]
_.pullAll(array, values)
This method is similar _.pull The difference is that this method receives an array of values to remove.
Note: different from _.difference , this method will change the array.
Import version
4.0.0
parameter
- array (Array): the array to modify.
- values (Array): the array of values to remove.
Return value
(Array): returns array.
example
var array = [1, 2, 3, 1, 2, 3]; _.pullAll(array, [2, 3]); console.log(array); // => [1, 1]
_.pullAllBy(array, values, [iteratee=_.identity])
This method is similar to _.pullAll The difference is that this method accepts an iteratee (iterative function) to call each value of array and values to generate a value. The generated values are compared. Iteratee will pass in a parameter: (value).
Note: different from _.differenceBy , this method will change the array.
Import version
4.0.0
parameter
- array (Array): the array to modify.
- values (Array): the array of values to remove.
- [iteratee=|. Identity] (array|function|object|string): iteratee (iterator) calls each element.
Return value
(Array): returns array
example
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }]; _.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x'); console.log(array); // => [{ 'x': 2 }]
_.pullAllWith(array, values, [comparator])
This method is similar to _.pullAll The difference is that this method accepts the comparison between elements and values in the array called by the comparator. The comparator will pass in two parameters: (arrVal, othVal).
Note: and _.differenceWith Different, this method will change the array.
Import version
4.6.0
parameter
- array (Array): the array to modify.
- values (Array): the array of values to remove.
- [comparator] (function): the comparator calls each element.
Return value
(Array): returns array.
example
var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }]; _.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual); console.log(array); // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]
_.pullAt(array, [indexes])
According to the index indexes, the corresponding elements in the array are removed and an array of removed elements is returned.
Note: and _.at Different, this method will change the array.
Import version
3.0.0
parameter
- array (Array): the array to modify.
- [indexes] (... (number|number[])): the index of the element to be removed.
Return value
(Array): returns a new array composed of removed elements.
example
var array = [5, 10, 15, 20]; var evens = _.pullAt(array, 1, 3); console.log(array); // => [5, 15] console.log(evens); // => [10, 20]
Array insert value sorting de duplication
_.sortedIndex(array, value) Insert at the smallest possible index position
Binary retrieval is used to determine that the value value should be inserted into the array at the smallest possible index position to ensure the sorting of the array.
Add version
0.1.0
parameter
- array (Array): the sorted array to check.
- Value (*): the value to be evaluated.
return
(number): returns the index position index where the value value should be inserted in the array array.
example
_.sortedIndex([30, 50], 40); // => 1
_.sortedIndexBy(array, value, [iteratee=_.identity])
This method is similar _.sortedIndex , except that it accepts an iteratee (iteration function), calls each array element, and compares the returned result with the value value to calculate the sorting. Iteratee will pass in a parameter: (value).
Add version
4.0.0
parameter
- array (Array): the sorted array to check.
- Value (*): the value to be evaluated.
- [iteratee=|. Identity] (array|function|object|string): iterates the function and calls each element.
return
(number): returns the index position index where the value value should be inserted in the array array.
example
var objects = [{ 'x': 4 }, { 'x': 5 }]; _.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; }); // => 0 // The `_.property` iteratee shorthand. _.sortedIndexBy(objects, { 'x': 4 }, 'x'); // => 0
_.sortedIndexOf(array, value)
This method is similar _.indexOf , except that it performs binary retrieval on the sorted array array.
Add version
4.0.0
parameter
- array (Array): the array to search.
- Value (*): the value of the search.
return
(number): returns the index position of the matching value, otherwise -1.
example
_.sortedIndexOf([4, 5, 5, 5, 6], 5); // => 1
_.sortedLastIndex(array, value)
This method is similar to _.sortedIndex , except that it returns the index position of the value value in the array as large as possible.
Add version
3.0.0
parameter
- array (Array): the sorted array to check.
- Value (*): the value to be evaluated.
return
(number): returns the index position index where the value value should be inserted in the array array.
example
_.sortedLastIndex([4, 5, 5, 5, 6], 5); // => 4
_.sortedLastIndexBy(array, value, [iteratee=_.identity])
This method is similar _.sortedLastIndex , except that it accepts an iteratee (iteration function), calls each array element, and compares the returned result with the value value to calculate the sorting. Iteratee will pass in a parameter: (value).
Add version
4.0.0
parameter
- array (Array): the sorted array to check.
- Value (*): the value to be evaluated.
- [iteratee=|. Identity] (array|function|object|string): iterates the function and calls each element.
return
(number): returns the index position index where the value value should be inserted in the array array.
example
var objects = [{ 'x': 4 }, { 'x': 5 }]; _.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; }); // => 1 // The `_.property` iteratee shorthand. _.sortedLastIndexBy(objects, { 'x': 4 }, 'x'); // => 1
_.sortedLastIndexOf(array, value)
This method is similar _.lastIndexOf , except that it performs binary retrieval on the sorted array array.
Add version
4.0.0
parameter
- array (Array): the array to search.
- Value (*): the value of the search.
return
(number): returns the index position of the matching value, otherwise -1.
example
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5); // => 3
_.sortedUniq(array) Sort de duplication
This method is similar _.uniq , except that it optimizes the sorted array.
Add version
4.0.0
parameter
- array (Array): the array to check.
return
(Array): returns a new non repeating array.
example
_.sortedUniq([1, 1, 2]); // => [1, 2]
_.sortedUniqBy(array, [iteratee]) Sort de duplication
This method is similar _.uniqBy , except that it optimizes the sorted array.
Add version
4.0.0
parameter
- array (Array): the array to check.
- [iteratee] (Function): iterates the function and calls each element.
return
(Array): returns a new non repeating array.
example
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor); // => [1.1, 2.3]
_.uniq(array) duplicate removal
Create a copy of the array array after de duplication. Used SameValueZero Make equivalence comparison. Only the first occurrence of an element is preserved.
Add version
0.1.0
parameter
- array (Array): the array to check.
return
(Array): returns a new array after de duplication.
example
_.uniq([2, 1, 2]); // => [2, 1]
_.uniqBy(array, [iteratee=_.identity])
This method is similar _.uniq , except that it accepts an iteratee (iterative function) and calls each element of each array to generate the criteria for uniqueness calculation. The iteratee call will pass in a parameter: (value).
Add version
4.0.0
parameter
- array (Array): the array to check.
- [iteratee=|. Identity] (array|function|object|string): iterates the function and calls each element.
return
(Array): returns a new array after de duplication.
example
_.uniqBy([2.1, 1.2, 2.3], Math.floor); // => [2.1, 1.2] // The `_.property` iteratee shorthand. _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }, { 'x': 2 }]
_.uniqWith(array, [comparator])
This method is similar _.uniq , except that it accepts a comparator call to compare each element of the arrays array. When calling the comparator, two parameters will be passed in: (arrVal, othVal).
Add version
4.0.0
parameter
- array (Array): the array to check.
- [comparator] (Function): compare functions and call each element.
return
(Array): returns a new array after de duplication.
example
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }]; _.uniqWith(objects, _.isEqual); // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]