const persons = [ {name: 'John', age: 20}, {name: 'Sara', age: 25}, {name: 'Jack', age: 30}, ]; const ages = _.pluck(persons, 'age'); console.log(ages); // Output: [20, 25, 30]
const books = [ {title: 'Harry Potter', author: 'J.K. Rowling'}, {title: 'The Da Vinci Code', author: 'Dan Brown'}, {title: 'To Kill a Mockingbird', author: 'Harper Lee'}, ]; const titles = _.pluck(books, 'title'); console.log(titles); // Output: ['Harry Potter', 'The Da Vinci Code', 'To Kill a Mockingbird']In this example, we have an array of book objects. We want to extract the titles of all these books. We use the pluck function to extract the titles from the books array. From the examples above, we can see that underscore pluck function is used to extract a list of property values from an array of objects. The underscore library provides a lot of useful functions like map, filter, reduce, etc. that make it easier to work with arrays and objects in JavaScript.