mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
add grab method to get elements in a collection that match the indexes in the provided list array
Former-commit-id: 18df81c229cab4acde8f8157df9bb1001a51e9db
This commit is contained in:
20
lodash.js
20
lodash.js
@@ -2175,6 +2175,25 @@
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the elements in the `collection` using the specified indexes
|
||||
* in the `list` array.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|String} collection The collection to iterate over.
|
||||
* @param {Array} list The array of indexes to grab.
|
||||
* @ returns {Array} Returns a new array of elements that matched the list array.
|
||||
* @example
|
||||
*
|
||||
* _.grab( ['a', 'b', 'c', 'd', 'e', 'f'], [0, 2, 5] );
|
||||
* // => ['a', 'c', 'f']
|
||||
*/
|
||||
function grab(collection, list) {
|
||||
return invoke(list, function(a){ return a[this]; }, collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys returned from running each element of
|
||||
* `collection` through a `callback`. The corresponding value of each key is an
|
||||
@@ -4239,6 +4258,7 @@
|
||||
lodash.forIn = forIn;
|
||||
lodash.forOwn = forOwn;
|
||||
lodash.functions = functions;
|
||||
lodash.grab = grab;
|
||||
lodash.groupBy = groupBy;
|
||||
lodash.initial = initial;
|
||||
lodash.intersection = intersection;
|
||||
|
||||
Reference in New Issue
Block a user