SharePoint Pals
 | Sign In
How to Use Underscore.js in Web Application
by Tarun Kumar Chatterjee 5 Sep 2016
Author
.Net – Technology Specialist
Visits   
Today  :   2     Total  :    1568

Underscore is an open source functional programming utility library for JavaScript. Underscore provides many useful functions for working with arrays or collections of JavaScript objects, including filtering, sorting and querying.

Underscore.JS is an useful JavaScript library for anyone familiar with the LINQ syntax. It is not a direct LINQ, but provides a very useful "LINQ-like" experience for anyone familiar with functional programming.

Using underscore is as simple as adding the JavaScript file to your project and referencing it on the page. Below is the command needs to run through package manager console to load underscore.js into the solution

PM> Install-Package underscore.js

Now let me show you few examples on basic usage of Underscore.js

First need to take the reference of Underscore.js in the project

<script src="Scripts/underscore.min.js"></script>

var data = [{name: 'Tarun2', age: 50},{name: 'Tarun1', age: 40}, {name: 'Tarun3', age: 60}];

Returns the maximum value in list:

JSON.stringify( _.max(data, function (val) { return val.age; }));

Returns the list sorted by name:

JSON.stringify(_.sortBy(data, 'name'));

Filter the list:

JSON.stringify(_.where(data, { name: 'Tarun1', age: 40 }));

New array of values by mapping each value in list:

JSON.stringify(_.map([1, 2, 3], function(num){ return num * 3; }))

Output will be [3,6,9]

List of Unique value in the list:

JSON.stringify(_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]))

Output will be: [1,2,3,101,10]

Hope this artifact will give you some basic ideas about the Underscore.js and its utility in the project. It will have lot of other functions, we can refer those from : http://underscorejs.org/ link.

Happy Coding

Tarun Kumar Chatterjee

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation