What is the difference between lodash's _.map and _.pluck?

自古美人都是妖i 提交于 2019-11-30 02:58:49

问题


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');

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


回答1:


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:

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]


来源:https://stackoverflow.com/questions/34765963/what-is-the-difference-between-lodashs-map-and-pluck

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