- lodash provides a utility library for JavaScript.it contains a set of methods and properties to work with data.
- lodash provides Array, Chain, Collection, Date, Function, lang, Number, Object, String ,Utlity, Object, String, Methods and Properties.
- documentation for lodash library can be found in following link https://lodash.com/docs.
- In this demo, “We will see some examples of using lodash utility methods”.
- The code for this demo is as follows:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>loadash utility library demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.1.0/lodash.min.js"></script>
</head>
<body>
<script>
var fruitArray = ["Apple","Orange","Mango","Grapes","Banana"];
console.log("First element in fruitArray: ", _.first(fruitArray));
console.log("Last element in fruitArray: ", _.last(fruitArray));
var studentArray=[
{name:"Sandeep",roll:34},
{name:"John",roll:31},
{name:"Smith",roll:20},
{name:"Rohan",roll:39},
{name:"Suresh",roll:23}
];
var chainStudentArray= _.chain(studentArray);
console.log("Chain: student with lowest roll: ",
chainStudentArray.sortBy('roll').first().value().name)
</script>
</body>
</html>
- The following screenshot shows the output of the above code in Chrome developer console: