Showing posts with label NodeJS-underscore. Show all posts
Showing posts with label NodeJS-underscore. Show all posts

Tuesday, 18 July 2017

node js underscore tutorial with examples

node js underscore tutorial with examples

Question: How to install underscore in nodeJS?
npm install underscore





Question: How to include underscore in nodeJS?
var _ = require('underscore');





Question: What is map in underscore? How to use? Give example?
Produces a new array of values by mapping each value in list through a function. The iterates is passed three arguments: the value, then the index (or key) of the iteration, and reference.


console.log(_.map([1, 2, 3], function(num){ return num * 2; })); //2, 4, 6
console.log(_.map([1, 2, 3], function(num){ return num * 3; })); //3,6,9



Question: Give an example of underscore.each?
console.log(_.each([1, 2, 3], function(value, key){
     console.log(key+'>>'+value)
 }));



Question: Give an example of underscore.reduce?
console.log(_.each([1, 2, 3], function(value, key){
     console.log(key+'>>'+value)
 }));



Question: Give an example of underscore.reduce?
console.log(_.each([1, 2, 3], function(value, key){
     console.log(key+'>>'+value)
 }));



Question: How to check if variable is empty OR Not?
_.isEmpty({});//true
_.isEmpty({'1'});//false



Question: How to create range values?
_.range(6);//[1,2,3,4,5,6]



Question: Can we use underscore for Web Client?
Yes, for this you need to include following JS.
https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js



Question: What are collections available in underscore?
each
map
reduce
reduceRight
find
filter
where
findWhere
reject
every
some
contains
invoke
pluck
max
min
sortBy
groupBy
indexBy
countBy
shuffle
sample
toArray
size
partition



Question: What are array are available in underscore?
first
initial
last
rest
compact
flatten
without
union
intersection
difference
uniq
zip
unzip
object
indexOf
lastIndexOf
sortedIndex
findIndex
findLastIndex
range



Question: What functions are available in underscore?
bind
bindAll
partial
memoize
delay
defer
throttle
debounce
once
after
before
wrap
negate
compose



Monday, 19 June 2017

underscore module with nodeJs tutorial

Underscore is a JavaScript library that provides a lot of useful helper functions to fast the node development. following are list of available array/object functions.

Question: What is underscore module?
Underscore is a JavaScript library that provides a lot of useful functional helpers without extending any built-in objects.



Question: How to install async module?
npm install underscore




Question: How to include request module in node project?
var _ = require("underscore");




Question: Give an example of each in underscore?
 
_.each(['Understand', 'underscore', 'Module'], function(value,pos){
    console.log(value+' stored at '+pos);    
});

Output
Understand stored at 0
underscore stored at 1
Module stored at 2




Question: What are other collections in underscore module?
each
map
reduce
reduceRight
find
filter
where
findWhere
reject
every
some
contains
invoke
pluck
max
min
sortBy
groupBy
indexBy
countBy
shuffle
sample
toArray
size
partition



Question: How to create a range array?
 
var lists=_.range(5);
console.log(_.first());




Question: How to get first nth element from array?
 
var lists=["One","two","three","four", "five"];
console.log(_.first(lists)); //One
console.log(_.first(lists,2)); //One two
console.log(_.first(lists,3)); //One two three




Question: How to check a variable is empty OR Not ?
var name=''
console.log(_.isEmpty(name));//true

name='Hello User'
console.log(_.isEmpty(name));//false




Question: What are array functions in underscore module?
first
initial
last
rest
compact
flatten
without
union
intersection
difference
uniq
zip
unzip
object
indexOf
lastIndexOf
sortedIndex
findIndex
findLastIndex
range



Question: How to get key value of an object data?
 
var lists={1:"One",2:"two",3:"three",4:"four", 5:"five"};
console.log(_.keys(lists)); //1,2,3,4,5




Question: What are the object functions in underscore module?
 
keys
allKeys
values
mapObject
pairs
invert
create
functions
findKey
extend
extendOwn
pick
omit
defaults
clone
tap
has
matcher
property
propertyOf
isEqual
isMatch
isEmpty
isElement
isArray
isObject
isArguments
isFunction
isString
isNumber
isFinite
isBoolean
isDate
isRegExp
isError
isNaN
isNull
isUndefined