Uses Of Underscore.js In Angular js

Posted By : Pradeep Singh Kushwah | 31-Jan-2018

I going to explain how we can use Underscore js in Angularjs, It is very useful when we want to filter, search etc on an Array Dataset.

 

Introduction:-

 

Underscore is a JavaScript library that offers a no. of helpful programming while not extending any integrated web app. It’s the answer to the question: “If I take a seat down in front of a clean HTML page, and want to start being productive immediately, what do I want?”  … and the tie to go together with jQuery's suit and determination's suspenders.

 

Underscore presents over a hundred of options that assist every your favoured ordinary helpful functions like map, filter out, invoke — moreover as a lot of specialised functionality: feature binding, javascript templating, creating brief indexes, deep equality checking out, and so forth.

 

Download the file from http://underscorejs.org/ and add to your project module, In angular controller add this code.

 

angular.module('underscore', []).factory('_', function() {
    return window._; // assumes underscore has already been loaded on the page
});

In controller:

loginApp.controller('newCalendarCtrl',['$scope','_',function($scope,_) {}]);

Examples:-

ForEach: Iterates over a list.

_.each([1, 2, 3], warning);

output: warning each no. in turn. 

Map: Create a new Array of the objects by mapping each object in a list.

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

output:[3,6,9]

Reduce: It can be used for the sum of all no. in an array.

var total = _.reduce([1, 2, 3], function(initial, num){ return initial + num; }, 0);

output: 6

Find: find a value in a list on matching condition.

var evenNumber = _.find([1, 2, 3, 4, 5, 6], function(value){ return value % 2 == 0; });

output:2

filter: It uses for filtering in a list on matching condition.

var evenvalue = _.filter([11, 12, 32, 41, 50, 64], function(number){ return number % 2 == 0; });

output:[2,4,6]

where: It is an advanced version of _.filter it can be use matching key and value pair.

_.where(listOfPlays, {writer: "shakespeare", year: 2018});

output:[{name:"leadership",writer:"shakespeare",year:2018},{name:"romeo-juliet",writer:"shakespeare",year:2018}]

 

About Author

Author Image
Pradeep Singh Kushwah

Pradeep is an accomplished Backend Developer with in-depth knowledge and hands-on experience in various cutting-edge technologies. He specializes in Core Java, Spring-Boot, Optaplanner, Angular, and databases such as MongoDB, Neo4j, Redis, and PostgreSQL. Additionally, he has worked with cloud services like AWS and Google Cloud, and he has experience with monitoring tools such as Datadog and Raygun. Pradeep has honed his skills in API Implementations, Integration, optimization, Webservices, Development Testings, and deployments, code enhancements, and has contributed to company values through his deliverables in various client projects, including Kairos, Slick Payroll, Captionlabs, and FarmQ. He is a creative individual with strong analytical skills and a passion for exploring and learning new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..