Open In App

Underscore.js _.pluck Function

The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects.

The _.pluck() function is used when we need to extract a list of a given property. Like we have to find out the name of all the students, then we can simply apply the _.pluck() function on the details of all the students. It will only extract the name from the details of all the stuf=dents and display it. The hence formed list will be an array of names only.



Syntax:

_.pluck(list, propertyName) 

Parameters: It takes two arguments:



Return values:
The returned value is an array of that property’s detail which we need to extract. The array will contain the elements in the same order in which they were in the list.

NOTE: These commands will not work in Google console or in firefox as for these, additional files need to be added which they didn’t have added.
So, add the below links to your HTML file and then run them.




<!-- Write HTML code here -->
<script type="text/javascript" src =
</script>

An example is shown below:




<html>
   
<head>
    <script type="text/javascript" src =
     </script>
</head>
   
<body>
    <script type="text/javascript">
         var users = [{id: 1, name:"harry"}, {id: 2, name:"jerry"},
                                  {id: 2, name:"jack"}];
    console.log(_.pluck(users, 'id'));
    </script>
</body>
   
</html>

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


Article Tags :