mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Add _.partition.
This commit is contained in:
61
lodash.js
61
lodash.js
@@ -1798,18 +1798,20 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function that aggregates a collection, creating an object composed
|
||||
* of keys generated from the results of running each element of the collection
|
||||
* Creates a function that aggregates a collection, creating an object or
|
||||
* array composed from the results of running each element of the collection
|
||||
* through a callback. The given `setter` function sets the keys and values
|
||||
* of the composed object.
|
||||
* of the composed object or array.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} setter The setter function.
|
||||
* @param {boolean} [retArray=false] A flag to indicate that the aggregator
|
||||
* function should return an array.
|
||||
* @returns {Function} Returns the new aggregator function.
|
||||
*/
|
||||
function createAggregator(setter) {
|
||||
function createAggregator(setter, retArray) {
|
||||
return function(collection, callback, thisArg) {
|
||||
var result = {};
|
||||
var result = retArray ? [[], []] : {};
|
||||
|
||||
callback = lodash.createCallback(callback, thisArg, 3);
|
||||
if (isArray(collection)) {
|
||||
@@ -4098,6 +4100,54 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of grouped elements, the first of which contains elements
|
||||
* the callback returns truey for, the second of which contains elements the
|
||||
* callback returns falsey for. The callback is bound to `thisArg` and invoked
|
||||
* with three arguments; (value, index|key, collection).
|
||||
*
|
||||
* If a property name is provided for `callback` the created "_.pluck" style
|
||||
* callback will return the property value of the given element.
|
||||
*
|
||||
* If an object is provided for `callback` the created "_.where" style callback
|
||||
* will return `true` for elements that have the properties of the given object,
|
||||
* else `false`.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [callback=identity] The function called
|
||||
* per iteration. If a property name or object is provided it will be used
|
||||
* to create a "_.pluck" or "_.where" style callback, respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `callback`.
|
||||
* @returns {Array} Returns a new array of grouped elements.
|
||||
* @example
|
||||
*
|
||||
* _.partition([1, 2, 3], function(num) { return num % 2; });
|
||||
* // => [[1, 3], [2]]
|
||||
*
|
||||
* _.partition([1.2, 2.3, 3.4], function(num) { return this.floor(num) % 2; }, Math);
|
||||
* // => [[1, 3], [2]]
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'barney', 'age': 36 },
|
||||
* { 'name': 'fred', 'age': 40, 'blocked': true },
|
||||
* { 'name': 'pebbles', 'age': 1 }
|
||||
* ];
|
||||
*
|
||||
* // using "_.where" callback shorthand
|
||||
* _.map(_.partition(characters, { 'age': 1 }), function(array) { return _.pluck(array, 'name'); });
|
||||
* // => [['pebbles'], ['barney', 'fred']]
|
||||
*
|
||||
* // using "_.pluck" callback shorthand
|
||||
* _.map(_.partition(characters, 'blocked'), function(array) { return _.pluck(array, 'name'); });
|
||||
* // => [['fred'], ['barney', 'pebbles']]
|
||||
*/
|
||||
var partition = createAggregator(function(result, value, key) {
|
||||
result[key ? 0 : 1].push(value);
|
||||
}, true);
|
||||
|
||||
/**
|
||||
* Retrieves the value of a specified property from all elements in the collection.
|
||||
*
|
||||
@@ -7452,6 +7502,7 @@
|
||||
lodash.pairs = pairs;
|
||||
lodash.partial = partial;
|
||||
lodash.partialRight = partialRight;
|
||||
lodash.partition = partition;
|
||||
lodash.pick = pick;
|
||||
lodash.pluck = pluck;
|
||||
lodash.property = property;
|
||||
|
||||
Reference in New Issue
Block a user