mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
optimized keys, values, and pluck
This commit is contained in:
@@ -298,8 +298,8 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
|
|||||||
<p id="pluck">
|
<p id="pluck">
|
||||||
<b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
|
<b class="header">pluck</b><code>_.pluck(list, propertyName)</code>
|
||||||
<br />
|
<br />
|
||||||
An optimized version of what is perhaps the most common use-case for
|
An convenient version of what is perhaps the most common use-case for
|
||||||
<b>map</b>: returning a list of property values.
|
<b>map</b>: extracting a list of property values.
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}];
|
||||||
|
|||||||
@@ -149,11 +149,9 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Optimized version of a common use case of map: fetching a property.
|
// Convenience version of a common use case of map: fetching a property.
|
||||||
_.pluck = function(obj, key) {
|
_.pluck = function(obj, key) {
|
||||||
var results = [];
|
return _.map(obj, function(value){ return value[key]; });
|
||||||
_.each(obj, function(value){ results.push(value[key]); });
|
|
||||||
return results;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return the maximum item or (item-based computation).
|
// Return the maximum item or (item-based computation).
|
||||||
@@ -357,18 +355,12 @@
|
|||||||
|
|
||||||
// Retrieve the names of an object's properties.
|
// Retrieve the names of an object's properties.
|
||||||
_.keys = function(obj) {
|
_.keys = function(obj) {
|
||||||
return _.reduce(obj, [], function(memo, value, key) {
|
return _.map(obj, function(value, key){ return key; });
|
||||||
memo.push(key);
|
|
||||||
return memo;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Retrieve the values of an object's properties.
|
// Retrieve the values of an object's properties.
|
||||||
_.values = function(obj) {
|
_.values = function(obj) {
|
||||||
return _.reduce(obj, [], function(memo, value) {
|
return _.map(obj, identity);
|
||||||
memo.push(value);
|
|
||||||
return memo;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extend a given object with all of the properties in a source object.
|
// Extend a given object with all of the properties in a source object.
|
||||||
|
|||||||
Reference in New Issue
Block a user