Explain Codes LogoExplain Codes Logo

What happened to Lodash _.pluck?

javascript
es6-alternatives
lodash-mixin
performance-optimization
Anton ShumikhinbyAnton Shumikhin·Mar 10, 2025
TLDR
// Time to swap _.pluck with its sophisticated cousin: _.map property shorthand _.map(array, 'property');

_.pluck has left the Lodash building in v4.0.0. In its stead, _.map with property shorthand comes in, suave and effortless, giving you the functionality you need in fewer characters.

// Swap '_.pluck(objects, 'key')' with this cool one-liner: const values = _.map(objects, 'key');

Example for the legends. Let's get users' ages:

// Age before beauty? We got you covered! const users = [{ 'name': 'Alice', 'age': 40 }, { 'name': 'Bob', 'age': 30 }]; const ages = _.map(users, 'age'); // [40, 30]

Travel light with the Lodash Changelog as it directs you from old roads to new bridges. While we're on a roll, the Lodash documentation awaits with a treasure trove of information, useful when that migration itch strikes.

Going native: ES6 alternatives

Lodash: a treasure trove of utilities, but with ES6 arrow functions pulling ahead, some methods can feel, well, antique. Presenting the native JavaScript way:

// When in Rome, write as Romans code: const extractedValues = objects.map(object => object.key);

This approach coexists with modern JavaScript coding standards. It makes reliance on external libraries optional, not mandatory.

The ghost of _.pluck: Extending Lodash with _.mixin

Old habits die hard, and if _.pluck was your go-to, resurrection is around the corner by using _.mixin like so:

_.mixin({ // _.pluck 2.0 loading... pluck: (array, key) => _.map(array, key) }); // Voila! Your very own _.pluck is back in action! const values = _.pluck(objects, 'key');

Embrace the spirit of _.pluck by extending its functionality to the new realm with a custom pluck method.

Staying in the loop: Library updates

Adapting is key to survival in a coding world. Libraries evolve, syntax becomes concise, and performance matter. Keep a keen eye on library updates and improve your code for the better.

Choosing ES6 map over Lodash

When does ES6 map outweigh Lodash?

  • You want simple array transformations, sans deep cloning.
  • Working with native arrays and crave chainability without summoning Lodash.
  • Performance optimization cravings? V8's got you covered with optimizations for native functions.

Heads-up: Potential issues

Watch out when migrating from _.pluck to _.map:

  • Ensure property names sit consistently across objects, as _.pluck plays ostrich with missing properties.
  • Reimagining objects into another shape? Go verbose with _.map, adding clarity over shorthand.

References