Javascript Lodash _.pluck 怎么了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35136306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:18:22  来源:igfitidea点击:

What happened to Lodash _.pluck?

javascriptlodash

提问by sfletche

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

我曾经使用过 Lodash _.pluck......我喜欢pluck ......

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

意识到 Lodash 不再支持pluck(从 Lodash 4.x 开始),我很难记住要使用什么来代替......

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'...

我去了文档,按 cmd-f,输入“pluck”,但我可怜的被遗弃的朋友甚至没有得到适当的提及……甚至没有一个“已被替换”……

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

有人可以提醒我我应该使用什么来代替吗?

回答by sfletche

Ah-ha! The Lodash Changelogsays it all...

啊哈!该Lodash更新日志说,这一切...

"Removed _.pluckin favor of _.mapwith iteratee shorthand"

“删除_.pluck以支持_.mapiteratee 速记”

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]

回答by Michael J. Calkins

There isn't a need for _.mapor _.plucksince ES6 has taken off.

不需要_.map_.pluck因为 ES6 已经起飞。

Here's an alternative using ES6 JavaScript:

这是使用 ES6 JavaScript 的替代方法:

clips.map(clip => clip.id)

clips.map(clip => clip.id)

回答by Dheeraj Nalawade

Use _.mapinstead of _.pluck. In the latest version the _.pluckhas been removed.

使用_.map代替_.pluck。在最新版本_.pluck中已删除。

回答by Richie Bendall

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

如果你真的想要_.pluck支持,你可以使用 mixin:

const _ = require("lodash")

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

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

因为map现在支持字符串(“迭代器”)作为参数而不是函数。

回答by PayteR

Or try pure ES6 nonlodash method like this

或者尝试像这样的纯 ES6 nonlodash 方法

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

var objects = [{ 'a': 1 }, { 'a': 2 }];
objects.reduce(reducer, [])