Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Where is _.pluck() in lodash version 4?
The _.pluck() method was removed from lodash version 4 because it provided the same functionality as _.map(). This change was part of lodash's effort to reduce redundancy and improve consistency.
What _.pluck() Did
In lodash 3.x, _.pluck() extracted property values from a collection of objects:
// Lodash 3.x syntax (no longer available) _.pluck(objects, 'propertyName')
Replacement: Using _.map()
In lodash 4+, use _.map() with a property path string to achieve the same result:
const _ = require('lodash');
const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }];
// Extract 'a' property from each object
const result = _.map(objects, 'a');
console.log(result);
[1, 2, 3]
Complex Property Paths
You can also extract nested properties using dot notation:
const _ = require('lodash');
const users = [
{ 'user': { 'name': 'Alice' } },
{ 'user': { 'name': 'Bob' } },
{ 'user': { 'name': 'Charlie' } }
];
// Extract nested property
const names = _.map(users, 'user.name');
console.log(names);
['Alice', 'Bob', 'Charlie']
Alternative: Native JavaScript
You can also use native JavaScript's map() method:
const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }];
// Using native JavaScript map
const result = objects.map(obj => obj.a);
console.log(result);
[1, 2, 3]
Comparison
| Method | Lodash Version | Syntax |
|---|---|---|
_.pluck() |
3.x only | _.pluck(collection, 'property') |
_.map() |
4.x+ | _.map(collection, 'property') |
Native map()
|
Not needed | collection.map(obj => obj.property) |
Conclusion
Replace _.pluck() with _.map() when upgrading to lodash 4+. Both methods provide identical functionality for extracting property values from object collections.
Advertisements
