334

I once used Lodash _.pluck...I loved pluck...

Realizing Lodash no longer supports pluck (as of Lodash 4.x), I'm struggling to remember what to use instead...

I went to the docs, hit cmd-f, typed 'pluck', but my poor abandoned friend is not even given a proper mention...not even a 'has been replaced by'...

Can someone please remind me what I'm supposed to use instead?

2

6 Answers 6

522

Ah-ha! The Lodash Changelog says it all...

"Removed _.pluck in favor of _.map with iteratee shorthand"

var objects = [{ 'a': 1 }, { 'a': 2 }];

// in 3.10.1
_.pluck(objects, 'a'); // → [1, 2]
_.map(objects, 'a'); // → [1, 2]

// in 4.0.0
_.map(objects, 'a'); // → [1, 2]
23
  • 124
    Could they not simply keep both...? I don't see the need to rename things just for the sake of renaming them. Commented Jun 21, 2016 at 18:06
  • 18
    It's not "renaming" though - map already existed - and pluck was simply removed. Map just happens to provide similar functionality with the same argument signature.
    – Chris
    Commented Aug 4, 2016 at 6:39
  • 61
    Why the hell would I want two doorbells on my house? If they both do the same thing, spare people the confusion and get rid of one. In my opinion, this is worth the breaking change. Get rid of extra junk, simplify the API, and people can learn to use the other doorbell. It's not like it's any harder to use. I wish more developers would remove cruft. +1 to the lodash team for introducing the breaking change (no sarcasm here, really). Commented Nov 6, 2016 at 5:53
  • 33
    Removing pluck just for funzies means nobody can ever safely update lodash. It's not like this is a compiled language... So annoying... Commented Jun 16, 2017 at 21:20
  • 10
    @ScottStafford That's why it's called a breaking change. If you don't bother reading the changelog when major versions change, then don't bother updating major versions. You can safely update within the same major version.
    – oligofren
    Commented Sep 25, 2017 at 15:05
90

There isn't a need for _.map or _.pluck since ES6 has taken off.

Here's an alternative using ES6 JavaScript:

clips.map(clip => clip.id)

5
  • 3
    No need for ES6 even: Array.prototype.map is in the ES5.1 standard. See browser support - it goes back as far as anyone needs to worry about in 2017, most probably!
    – davnicwil
    Commented Feb 21, 2017 at 12:31
  • 2
    @davnicwil Array.prototype.map is ES5.1, sure, but the cute arrow syntax => for anonymous functions isn't. ;)
    – 00dani
    Commented Feb 23, 2017 at 22:53
  • 3
    @00Dani good point! My eyes completely skipped over the fat arrow. Ah well,clips.map(function (clip) { return clip.id }) still isn't so bad I guess ;-)
    – davnicwil
    Commented Feb 24, 2017 at 8:55
  • 13
    You can actually make it shorter if using ES6 clips.map(({id}) => id)
    – Dr.Pil
    Commented Jul 4, 2017 at 9:22
  • 1
    This is very true but it's useful to know what happened with _.pluck as some pre ES6 projects might make use of lodashe's methods. :)
    – csalmeida
    Commented Nov 27, 2017 at 12:50
21

Use _.map instead of _.pluck. In the latest version the _.pluck has been removed.

4

If you really want _.pluck support back, you can use a mixin:

const _ = require("lodash")

_.mixin({
    pluck: _.map
})

Because map now supports a string (the "iterator") as an argument instead of a function.

2
  • pluck != map. Pluck lets you pass in a string and is the same as giving function (record) { return(record[that_string]) } Commented Jan 13, 2020 at 20:09
  • @BrianUnderwood Thanks! I'll reiterate: _.map can be used as a functional equalivent if used correctly. Commented Jan 13, 2020 at 23:58
0

For plucking single or multiple properties:

_.mixin({
    properties: (paths) =>
            (obj) => paths.reduce((memo, path) => [...memo, obj[path]], []),
    pluck: (obj, ...keys) => _.map(obj, _.flatten(keys).length > 1
                                    ? _.properties(_.flatten(keys))
                                    : (o) => o[keys[0]])
})
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];

// underscore compatible usage
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]

// multiple property usage
_.pluck(stooges, 'name', 'age')
=> [["moe",40], ["larry",50], ["curly",60]]

// alternate usage
_.pluck(stooges, ['name', 'age']) 
=> [["moe",40], ["larry",50], ["curly",60]]
-2

Or try pure ES6 nonlodash method like this

const reducer = (array, object) => {
  array.push(object.a)
  return array
}

var objects = [{ 'a': 1 }, { 'a': 2 }];
objects.reduce(reducer, [])
1
  • 3
    This is unnecessarily complicated, when you can do objects.map(object => object.a). Commented Apr 9, 2020 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.