- Underscore.JS - Discussion
- Underscore.JS - Useful Resources
- Underscore.JS - Quick Guide
- Underscore.JS - Chaining
- Underscore.JS - Utilities
- Underscore.JS - Comparing Objects
- Underscore.JS - Updating Objects
- Underscore.JS - Mapping Objects
- Underscore.JS - Functions
- Underscore.JS - Processing Array
- Underscore.JS - Iterating Array
- Underscore.JS - Processing Collection
- Underscore.JS - Iterating Collection
- Underscore.JS - Environment Setup
- Underscore.JS - Overview
- Underscore.JS - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Underscore.JS - Quick Guide
Underscore.JS - Overview
Underscore.JS is a popular javascript based pbrary which provides 100+ functions to faciptate web development. It provides helper functions pke map, filter, invoke as well as function binding, javascript templating, deep equapty checks, creating indexes and so on. Underscore.JS can be used directly inside a browser and also with Node.js.
Working with objects using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with them. Underscore comes with lots of features that eases your work with objects.
Underscore.JS is an open source project and you can easily contribute to the pbrary and add features in the form of plugins and make it available on GitHub and in Node.js.
Features
Let us understand in detail all the important features available with Underscore −
Collections
Underscore.JS provides various functions for collections pke each, map, reduce which are used to apply an operation on each item of a collection. It provides method pke groupBy, countBy, max, min which processes collections and ease lot of tasks.
Arrays
Underscore.JS provides various functions for arrays pke to iterate and process arrays pke first, initial, lastIndexOf, intersection, difference etc.
Functions
Underscore.JS provides functions such as bind, delay, before, after etc.
Objects
Underscore.JS provides functions to manipulate objects, to map objects and comparing objects. For example, keys, values, extends, extendsOwn, isEqual, isEmpty etc.
Utipties
Underscore.JS provides various utipties methods pke noConfpct, random, iteratee, escape etc.
Chaining
Underscore.JS provides chaining methods as well pke chain, value.
In subsequent chapters, we ll cover importants functions of Underscore.JS
Underscore.JS - Environment Setup
In this chapter, you will learn in detail about setting up the working environment of Underscore.JS on your local computer. Before you begin with working on Underscore.JS, you need to have the access to the pbrary. You can access its files in any of the following methods −
Method 1: Using Underscore.JS File in Browser
In this method, we are going to need Underscore.JS file from its official website and will use it directly in the browser.
Step 1
As a first step, go to the official website of Underscore.JS
.Observe that there is a download option available which gives you the latest underscore-min.js file
available. Right Cpck on the pnk and choose save as. Note that the file is available with and without minification.Step 2
Now, include underscore-min.js inside the script tag and start working with Underscore.JS. For this, you can use the code given below −
<script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script>
Given here is a working example and its output for a better understanding −
Example
<html> <head> <title>Underscore.JS - Working Example</title> <script type = "text/JavaScript" src = "https://underscorejs.org/underscore-min.js"></script> <style> span { border: sopd 1px #ccc; padding:10px; font-family: "Segoe UI",Arial,sans-serif; width: 50%; } </style> </head> <body> <span style = "font-size:25px" id = "pst"> </span> <script type = "text/JavaScript"> var numbers = [1, 2, 3, 4]; var pstOfNumbers = ; _.each(numbers, function(x) { pstOfNumbers += x + }); document.getElementById("pst").innerHTML = pstOfNumbers; </script> </body> </html>
Output
Method 2: Using Node.js
If you are opting for this method, make sure you have Node.js and npm installed on your system. You can use the following command to install Underscore.JS −
npm install underscore
You can observe the following output once Underscore.JS is successfully installed −
+ underscore@1.10.2 added 1 package from 1 contributor and audited 1 package in 6.331s found 0 vulnerabipties
Now, to test if Underscore.JS works fine with Node.js, create the file tester.js and add the following code to it −
var _ = require( underscore ); var numbers = [1, 2, 3, 4]; var pstOfNumbers = ; _.each(numbers, function(x) { pstOfNumbers += x + }); console.log(pstOfNumbers);
Save the above program in tester.js. The following commands are used to compile and execute this program.
Command
>node tester.js
Output
1 2 3 4
Underscore.JS - Iterating Collections
Underscore.JS has many easy to use methods which helps in iterating Collections. This chapter discusses them in detail.
Underscore.JS provides various methods to iterate the Collections as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.each(pst, iteratee, [context]) |
2 | _.map(pst, iteratee, [context]) |
3 | _.reduce(pst, iteratee, [memo], [context]) |
4 | _.reduceRight(pst, iteratee, [memo], [context]) |
5 | _.find(pst, predicate, [context]) |
6 | _.filter(pst, predicate, [context]) |
7 | _.where(pst, properties) |
8 | _.findWhere(pst, properties) |
9 | _.reject(pst, predicate, [context]) |
10 | _.every(pst, [predicate], [context]) |
11 | _.some(pst, [predicate], [context]) |
Underscore.JS - Processing Collections
Underscore.JS has many easy to use methods which helps in processing Collections. This chapter discusses them in detail.
Underscore.JS provides various methods to process the Collections as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.contains(pst, value, [fromIndex]) |
2 | _.invoke(pst, methodName, *arguments) |
3 | _.pluck(pst, propertyName) |
4 | _.max(pst, [iteratee], [context]) |
5 | _.min(pst, [iteratee], [context]) |
6 | _.sortBy(pst, iteratee, [context]) |
7 | _.groupBy(pst, iteratee, [context]) |
8 | _.indexBy(pst, iteratee, [context]) |
9 | _.countBy(pst, iteratee, [context]) |
10 | _.shuffle(pst) |
11 | _.sample(pst, [n]) |
12 | _.toArray(pst) |
13 | _.size(pst) |
14 | _.partition(pst, predicate) |
15 | _.compact(pst) |
Underscore.JS - Iterating Array
Underscore.JS has many easy to use methods which helps in iterating Arrays. This chapter discusses them in detail.
Underscore.JS provides various methods to iterate the Arrays as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.first(array, [n]) |
2 | _.initial(array, [n]) |
3 | _.last(array, [n]) |
4 | _.rest(array, [index]) |
5 | _.indexOf(array, value, [isSorted]) |
6 | _.lastIndexOf(array, value, [fromIndex]) |
7 | _.sortedIndex(array, value, [iteratee], [context]) |
8 | _.findIndex(array, predicate, [context]) |
9 | _.findLastIndex(array, predicate, [context]) |
Underscore.JS - Processing Array
Underscore.JS has many easy to use methods which helps in processing Arrays. This chapter discusses them in detail.
Underscore.JS provides various methods to process the Arrays as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.flatten(array, [shallow]) |
2 | _.without(array, *values) |
3 | _.union(*arrays) |
4 | _.intersection(*arrays) |
5 | _.difference(array, *others) |
6 | _.uniq(array, [isSorted], [iteratee]) |
7 | _.zip(*arrays) |
8 | _.unzip(array) |
9 | _.object(pst, [values]) |
10 | _.chunk(array, length) |
11 | _.range([start], stop, [step]) |
Underscore.JS - Functions
Underscore.JS has many easy to use methods which helps in handpng functions. This chapter discusses them in detail.
Underscore.JS provides various methods to handle functions as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.bind(function, object, *arguments) |
2 | _.partial(function, *arguments) |
3 | _.memoize(function, [hashFunction]) |
4 | _.delay(function, wait, *arguments) |
5 | _.once(function) |
6 | _.before(count, function) |
7 | _.wrap(function, wrapper) |
8 | _.negate(predicate) |
9 | _.compose(*functions) |
Underscore.JS - Mapping Objects
Underscore.JS has many easy to use methods which helps in mapping objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object mapping as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.keys(object) |
2 | _.allKeys(object) |
3 | _.values(object) |
4 | _.mapObject(object, iteratee, [context]) |
5 | _.pairs(object) |
6 | _.invert(object) |
7 | _.create(prototype, props) |
8 | _.functions(object) |
9 | _.findKey(object, predicate, [context]) |
Underscore.JS - Updating Objects
Underscore.JS has many easy to use methods which helps in updating objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object updates as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.extend(destination, *sources) |
2 | _.pick(object, *keys) |
3 | _.omit(object, *keys) |
4 | _.defaults(object, *defaults) |
5 | _.clone(object) |
6 | _.tap(object, interceptor) |
7 | _.has(object, key) |
8 | _.property(path) |
9 | _.propertyOf(object) |
Underscore.JS - Comparing Objects
Underscore.JS has many easy to use methods which helps in comparing objects. This chapter discusses them in detail.
Underscore.JS provides various methods to handle object comparison as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.matcher(attrs) |
2 | _.isEqual(object, other) |
3 | _.isMatch(object, properties) |
4 | _.isEmpty(object) |
5 | _.isArray(object) |
6 | _.isObject(value) |
7 | _.isArguments(object) |
8 | _.isFunction(object) |
9 | _.isString(object) |
10 | _.isNumber(object) |
11 | _.isFinite(object) |
12 | _.isBoolean(object) |
13 | _.isDate(object) |
14 | _.isRegExp(object) |
15 | _.isError(object) |
16 | _.isSymbol(object) |
17 | _.isMap(object) |
18 | _.isWeakMap(object) |
19 | _.isSet(object) |
20 | _.isWeakSet(object) |
21 | _.isNaN(object) |
22 | _.isNull(object) |
23 | _.isUndefined(value) |
Underscore.JS - Utipties
Underscore.JS has many easy to use utipty methods. This chapter discusses them in detail.
Underscore.JS provides various utipty methods as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.identity(value) |
2 | _.constant(value) |
3 | _.noop() |
4 | _.times(n, iteratee, [context]) |
5 | _.random(min, max) |
6 | _.mixin(object) |
7 | _.iteratee(value, [context]) |
8 | _.uniqueId([prefix]) |
9 | _.escape(string) |
10 | _.unescape(string) |
11 | _.result(object, property, [defaultValue]) |
12 | _.now() |
13 | _.template(templateString, [settings]) |
Underscore.JS - Chaining
Underscore.JS has methods to create a chain of methods and then retrive their effective result. This chapter discusses them in detail.
Underscore.JS provides various utipty methods as psted below −
Sr.No. | Method & Syntax |
---|---|
1 | _.chain(object) |
2 | _.chain(obj).value() |