Move _.pluck to the Arrays category.

Former-commit-id: a3ba36c1c320c8685c25fcb0bbe16ba2baeb6731
This commit is contained in:
John-David Dalton
2012-06-06 22:28:17 -04:00
parent 51a459fe48
commit 5e8c373bf4
2 changed files with 33 additions and 30 deletions

View File

@@ -117,7 +117,7 @@
'once': [], 'once': [],
'partial': [], 'partial': [],
'pick': [], 'pick': [],
'pluck': ['createIterator'], 'pluck': [],
'range': [], 'range': [],
'reduce': ['createIterator'], 'reduce': ['createIterator'],
'reduceRight': ['keys'], 'reduceRight': ['keys'],

View File

@@ -333,7 +333,7 @@
} }
}; };
/** Reusable iterator options for `map`, `pluck`, and `values` */ /** Reusable iterator options for `map` and `values` */
var mapIteratorOptions = { var mapIteratorOptions = {
'init': '', 'init': '',
'exit': 'if (!collection) return []', 'exit': 'if (!collection) return []',
@@ -710,34 +710,6 @@
*/ */
var map = createIterator(baseIteratorOptions, mapIteratorOptions); var map = createIterator(baseIteratorOptions, mapIteratorOptions);
/**
* Retrieves the value of a specified property from all values in a `collection`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object} collection The collection to iterate over.
* @param {String} property The property to pluck.
* @returns {Array} Returns a new array of property values.
* @example
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* ];
*
* _.pluck(stooges, 'name');
* // => ['moe', 'larry', 'curly']
*/
var pluck = createIterator(mapIteratorOptions, {
'args': 'collection, property',
'inLoop': {
'array': 'result[index] = collection[index][property]',
'object': 'result.push(collection[index][property])'
}
});
/** /**
* Boils down a `collection` to a single value. The initial state of the * Boils down a `collection` to a single value. The initial state of the
* reduction is `accumulator` and each successive step of it should be returned * reduction is `accumulator` and each successive step of it should be returned
@@ -1418,6 +1390,37 @@
return result; return result;
} }
/**
* Retrieves the value of a specified property from all elements in `array`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to iterate over.
* @param {String} property The property to pluck.
* @returns {Array} Returns a new array of property values.
* @example
*
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 },
* { 'name': 'curly', 'age': 60 }
* ];
*
* _.pluck(stooges, 'name');
* // => ['moe', 'larry', 'curly']
*/
function pluck(array, property) {
var index = -1,
length = array.length,
result = Array(length);
while (++index < length) {
result[index] = array[index][property];
}
return result;
}
/** /**
* Creates an array of numbers (positive and/or negative) progressing from * Creates an array of numbers (positive and/or negative) progressing from
* `start` up to but not including `stop`. This method is a port of Python's * `start` up to but not including `stop`. This method is a port of Python's