how to use the pluck function from the underscore library in javascript

The pluck function from the underscore library is used to extract an array of values for a specified property from a collection of objects.

Here's an example of how to use pluck:

index.tsx
const _ = require('underscore');

const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Mary' },
  { id: 3, name: 'Bob' }
];

const names = _.pluck(users, 'name');

console.log(names); // ['John', 'Mary', 'Bob']
219 chars
12 lines

In this example, we have an array of user objects with id and name properties. We pass this array to the pluck function along with the string 'name' indicating that we want to extract the name property of each object. The resulting array names contains the values ['John', 'Mary', 'Bob'].

Note that the pluck function returns a new array and does not modify the original collection.

gistlibby LogSnag