What happened to Lodash _.pluck?

前端 未结 5 2303
情深已故
情深已故 2020-11-29 19:21

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

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

相关标签:
5条回答
  • 2020-11-29 20:05

    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, [])
    
    0 讨论(0)
  • 2020-11-29 20:12

    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.

    0 讨论(0)
  • 2020-11-29 20:18

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

    0 讨论(0)
  • 2020-11-29 20:19

    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]
    
    0 讨论(0)
  • 2020-11-29 20:20

    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)

    0 讨论(0)
提交回复
热议问题