What is underscore.js and how to use underscore.js in Node.js?
underscore.js allow us to write cleaner code which is easy for another developer to read and understand.
underscore.js is written by Jeremy Ashkens . underscore .js is an open-source library . underscore collection is denoted by ( _. ).
Collection function in underscore.js?
collection function are those function that works on array , Objects or nodelist .underscore allows us to use numbers of a collection in your nodelist , object , array . following are some underscore.js collection -
Function name Prototype
each : _.each(list, iteratee, [context])
map : _.map(list, iteratee, [context])
find : _.find(list, predicate, [context])
filter : _.filter(list, predicate, [context])
reject : _.reject(list, predicate, [context])
How to use underscore .js in node js ?
For installing underscore.js in node js you have to install underscore.js library from NPM
use the following command for installing underscore.js in your node.js project
Step1 - npm install underscore or npm i underscore.
step 2 - include underscore.js library in your existing project by following - const _. = require("underscore.js)
there are numbers of collections provides by underscore.js List of some most used collection here -
1) Each ( _.each ) -
2) MAP (_.map )
example how to use each collection method is node js -
Advantages of underscore.js
1) underscore .js is an open source libaray .
2) we can use it with frontend(Angular,React) and backend(Node js ) both.
3) There are a number of collections available in underscore.js.
4) Clean and easy to understand code.
5) Reduce code complexity by using the collection's rather than repetitive code and Algorithm.
underscore.js is an open-source library that provides a collection that helps us to work in the actual implementation of the project rather than working on repetitive task or algorithm . its make code cleaner and easy to understand and reduce code complexity.