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
What is the replacement of lodash pluck() method?
Lodash's pluck() method was removed in version 4.0 because it provided the same functionality as the map() method. The pluck() method was used to extract property values from objects in an array.
Using _.map() as Replacement
You can replace _.pluck() with _.map() using the property shorthand syntax:
import _ from 'lodash';
const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }];
console.log(_.map(objects, 'a'));
[1, 2, 3]
Using Native Array.map()
For modern JavaScript, you can use the native Array.map() method without lodash:
const objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }];
const result = objects.map(obj => obj.a);
console.log(result);
[1, 2, 3]
Extracting Nested Properties
For nested properties, both approaches work similarly:
const users = [
{ name: { first: 'John', last: 'Doe' } },
{ name: { first: 'Jane', last: 'Smith' } }
];
// Using lodash
// console.log(_.map(users, 'name.first'));
// Using native JavaScript
const firstNames = users.map(user => user.name.first);
console.log(firstNames);
['John', 'Jane']
Comparison
| Method | Bundle Size | Browser Support |
|---|---|---|
_.map() |
Adds lodash dependency | All browsers |
Array.map() |
Native, no dependency | ES5+ (IE9+) |
Conclusion
Replace _.pluck() with _.map() for lodash users, or use native Array.map() for modern JavaScript without external dependencies. Both provide the same property extraction functionality.
Advertisements
