lodash的_.map和_.pluck有什么区别? [英] What is the difference between lodash's _.map and _.pluck?

查看:455
本文介绍了lodash的_.map和_.pluck有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,任何人都可以分辨出区别:

I have the following code, can anyone tell the difference:

const _ = require('lodash');

const arr = [
    {'fname':'Ali', 'lname': 'Yousuf'},
    {'fname': 'Uzair', 'lname': 'Ali'},
    {'fname': 'Umair', 'lname': 'Khan'}
];

_.map(arr, 'fname');
_.pluck(arr, 'fname');

输出是相同的,并且两个函数都没有对arr进行突变.

The output is the same, and both functions are not mutating arr.

推荐答案

在您使用它们的方式中,它们基本上会执行相同的操作.这就是为什么从Lodash v4.0.0中删除了.pluck(),而赞成使用带有字符串作为第二个参数的.map()的原因.

In the way you're using them, they basically do the same. That's why .pluck() was removed from Lodash v4.0.0 in favor of using .map() with a string as second argument.

以下是更改日志的相关摘录:

Here's the relevant excerpt from the changelog:

以iteratee的速记方式将_.pluck取而代之的是_.map

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]

这篇关于lodash的_.map和_.pluck有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆