What happened to Lodash _.pluck?

£可爱£侵袭症+ 提交于 2019-11-26 10:22:27

问题


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?


回答1:


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]



回答2:


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)




回答3:


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




回答4:


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, [])


来源:https://stackoverflow.com/questions/35136306/what-happened-to-lodash-pluck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!