简体   繁体   中英

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

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

let _ = require('lodash');

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

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] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM