diff --git a/build.js b/build.js index ba7c4fac6..c52cd0eda 100755 --- a/build.js +++ b/build.js @@ -117,7 +117,7 @@ 'once': [], 'partial': [], 'pick': [], - 'pluck': ['createIterator'], + 'pluck': [], 'range': [], 'reduce': ['createIterator'], 'reduceRight': ['keys'], diff --git a/lodash.js b/lodash.js index 17e0cfcc8..e51779365 100644 --- a/lodash.js +++ b/lodash.js @@ -333,7 +333,7 @@ } }; - /** Reusable iterator options for `map`, `pluck`, and `values` */ + /** Reusable iterator options for `map` and `values` */ var mapIteratorOptions = { 'init': '', 'exit': 'if (!collection) return []', @@ -710,34 +710,6 @@ */ 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 * reduction is `accumulator` and each successive step of it should be returned @@ -1418,6 +1390,37 @@ 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 * `start` up to but not including `stop`. This method is a port of Python's