tag:blogger.com,1999:blog-87599304834796181372024-10-24T17:40:56.078-07:00Underscore Array FunctionsAnonymoushttp://www.blogger.com/profile/03648896726559627150noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-8759930483479618137.post-28276876434091173482014-01-14T23:17:00.002-08:002014-01-20T22:39:26.486-08:00UnderScore.JS Tutorial [ARRAY FUNCTION EXAMPLES]<br />
<br />
<li>/**</li>
<li> * Author Piyush Arya</li>
<li>* Email : arya.piyush87@gmail.com</li>
<li> * Module :- UnderScore.js Tutorial</li>
<li> * Date Created :- Jan 2014</li>
<li> */</li>
<li>var contactList = {</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"returnList" : [{</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"name" : "NAME",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"password" : "dummy",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"time" : "today",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"email" : "a.com"</li>
<li><br /></li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>}],</li>
<li><br /></li>
<li>};</li>
<li><br /></li>
<li>window.logger = function(param) {</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>return console.log(param);</li>
<li><br /></li>
<li>};</li>
<li><br /></li>
<li>var arr = ["1", "2", "3", "4", "5"], every = [true, 0, "hello"],
invokeSort = [[3, 6, 5, 2, 4, 1], ["b", "d", "c", "a"]], invokeupper =
["b", "d", "c", "a"], arrColl = [1, 2, 3, 4, 5], json = [{</li>
<li><br /></li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"name" : "piyush",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"email" : "demo@gmail.com",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"age" : 25</li>
<li>}, {</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"name" : "piyush",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"email" : "demo@yahoo.com",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"age" : 26</li>
<li>}, {</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"name" : "arya",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"email" : "demo@hotmail.com",</li>
<li><span class="Apple-tab-span" style="white-space: pre;"> </span>"age" : 27</li>
<br />
<br />
<li>}];</li>
<br />
/* ************* ARRAY FUNCTION EXAMPLES ******************** */<br />
<br />
<br />
/* **** UNCOMMENT LOGGER TO SEE THE RESULT IN CONSOLE***** */<br />
<br />
<br />
/* ------------------first Function Starts------------------------------------ */<br />
<br />
var firstDemo = _.first(arrColl);<br />
//var firstDemo = _.first(arrColl,3);<br />
// 1 2 3 when passed 3 as parameter<br />
//logger("firstDemo --> " + firstDemo);<br />
// Returns the first element of an array<br />
<br />
/* -----------------------first Function end--------------------------- */<br />
<br />
<br />
/* -----------------------initial Function Starts------------------------ */<br />
<br />
var initialDemo = _.initial(arrColl);<br />
//var initialDemo = _.first(arrColl,2);<br />
// 1 2 when pass 2 as parameter<br />
//logger("initialDemo --> " + initialDemo);<br />
// Returns everything but the last entry of the array<br />
<br />
/* -------------------------initial Function end------------------------- */<br />
<br />
<br />
/* -------------------------last Function Starts------------------------ */<br />
<br />
var lastDemo = _.last(arrColl);<br />
//var lastDemo = _.last(arrColl,3);<br />
// 3 4 5 when pass 3 as parameter<br />
//logger("lastDemo --> " + lastDemo);<br />
// Returns the last element of an array<br />
<br />
/* --------------------------last Function end------------------------ */<br />
<br />
<br />
/* --------------------------rest Function Starts------------------------ */<br />
<br />
//var restDemo = _.rest(arrColl);<br />
var restDemo = _.rest(arrColl, 3);<br />
// 4 5 when pass 3 as parameter<br />
//logger("restDemo --> " + restDemo);<br />
// <span style="font-family: Helvetica Neue, Helvetica, Arial;"><span style="font-size: 14px; line-height: 22px;">Returns the rest of the elements in an array</span></span><br />
<br />
/* ---------------------------rest Function end------------------------------- */<br />
<br />
<br />
/* ---------------------------compact Function Starts----------------------- */<br />
<br />
var compactDemo = _.compact([0, 1, false, 2, '', 3, null, undefined, '1', '2', 4]);<br />
//logger("compactDemo --> " + compactDemo);<br />
// Returns a copy of the array with all falsy values removed<br />
<br />
/* -----------------------compact Function end------------------------------ */<br />
<br />
<br />
/* ----------------------flatten Function Starts------------------------------- */<br />
<br />
var flattenDemo = _.flatten([1, [2], [3, [[4]]]], true);<br />
//logger("flattenDemo --> " + flattenDemo);<br />
// Flattens a nested array (the nesting can be to any depth).<br />
<br />
/* -----------------------flatten Function end------------------------------- */<br />
<br />
<br />
/* ----------------------without Function Starts------------------------- */<br />
<br />
var withoutDemo = _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);<br />
//logger("withoutDemo --> " + withoutDemo);<br />
// Returns a copy of the array with all instances of the values removed.<br />
<br />
/* ------------------------without Function end------------------------------- */<br />
<br />
<br />
<br />
/* -------------------------union Function Starts------------------------------- */<br />
<br />
var unionDemo = _.union([1, 2, 3], [101, 2, 1, 10], [2, 1, 9]);<br />
//logger("unionDemo --> " + unionDemo);<br />
// Computes the union of the passed-in arrays: the list of unique items.<br />
<br />
/* -------------------------union Function end---------------------------------- */<br />
<br />
<br />
/* ------------------------intersection Function Starts--------------------------- */<br />
<br />
var intersectionDemo = _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);<br />
//logger("intersectionDemo --> " + intersectionDemo);<br />
// Computes the list of values that are the intersection of all the arrays<br />
<br />
/* --------------------------intersection Function end---------------------------- */<br />
<br />
/* -------------------------difference Function Starts---------------------------- */<br />
<br />
var differenceDemo = _.difference([1, 2, 3, 4, 5], [5, 2, 10]);<br />
//logger("differenceDemo --> " + differenceDemo);<br />
// Similar to without, but returns the values from array that are not present in the other arrays.<br />
<br />
/* -----------------------difference Function end--------------------------------- */<br />
<br />
<br />
/* ----------------------uniq Function Starts--------------------------------------- */<br />
<br />
var uniqDemo = _.uniq([1, 2, 1, 4, 1, 3]);<br />
//Produces a duplicate-free version of the array, using === to test object equality.<br />
//logger("uniqDemo --> " + uniqDemo);<br />
<br />
/* ---------------------uniq Function end--------------------------------------------- */<br />
<br />
/* ----------------------zip Function Starts-------------------------------------------- */<br />
<br />
var zipDemo = _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);<br />
//Merges together the values of each of the arrays with the values at the corresponding position.<br />
//logger("zipDemo --> " + zipDemo);<br />
<br />
/* ------------------------zip Function end-------------------------------------------- */<br />
<br />
/* ------------------------object Function Starts-------------------------------------- */<br />
<br />
var objectDemo = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);<br />
//var objectDemo = _.object([['moe', 30], ['larry', 40], ['curly', 50]]);<br />
//Converts arrays into objects. If duplicate keys exist, the last value wins.<br />
//logger("objectDemo --> " + JSON.stringify(objectDemo));<br />
<br />
/* -------------------------object Function end---------------------------------------- */<br />
<br />
<br />
/* ------------------------- -indexOfDemo Function Starts-------------------------- */<br />
var indexOfDemo = _.indexOf([1, 2, 3], 2);<br />
//Returns the index at which value can be found in the array, or -1 if value is not present in the array<br />
// Starts with 0<br />
//logger("indexOfDemo --> " + indexOfDemo);<br />
<br />
/* ---------------------------indexOfDemo Function end----------------------------- */<br />
<br />
<br />
/* --------------------------lastIndexOfDemo Function Starts------------------------- */<br />
<br />
var lastIndexOfDemo = _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);<br />
// starts with 0<br />
//Returns the index of the last occurrence of value in the array, or -1 if value is not present.<br />
//logger("lastIndexOfDemo --> " + lastIndexOfDemo);<br />
<br />
/* --------------------------lastIndexOfDemo Function end----------------------------- */<br />
<br />
<br />
/* -------------------------sortedIndex Function Starts---------------------------------- */<br />
<br />
var sortedIndexDemo = _.sortedIndex([10, 20, 30, 40, 50], 55);<br />
// starts with 0<br />
//Uses a binary search to determine the index at which the value should be inserted into the list in order to //maintain the list's sorted order<br />
//logger("sortedIndexDemo --> " + sortedIndexDemo);<br />
<br />
/* --------------------------sortedIndex Function end------------------------------------ */<br />
<br />
/* ---------------------------range Function Starts---------------------------------------- */<br />
<br />
var rangeDemo = _.range(10);<br />
//var rangeDemo = _.range(1, 11);<br />
//var rangeDemo =_.range(0, 30, 5);<br />
//var rangeDemo =_.range(0, -10, -1);<br />
//var rangeDemo =_.range(0);<br />
//logger("rangeDemo --> " + rangeDemo);<br />
<br />
/* --------------------------range Function end------------------------------------------ */<br />
<br />
<a href="http://piyusharya12.blogspot.in/" target="_blank">Click Here For Underscore Collections Functions with Examples</a><br />
<div>
<br /></div>
<div>
<a href="http://underscorefunctiontutorial.blogspot.in/2014/01/underscore-function-tutorial.html" target="_blank">Click Here For Underscore Functions with Examples</a></div>
Anonymoushttp://www.blogger.com/profile/03648896726559627150noreply@blogger.com1