Tuesday, January 14, 2014

UnderScore.JS Tutorial [ARRAY FUNCTION EXAMPLES]



  • /**
  •  * Author Piyush Arya
  • * Email : arya.piyush87@gmail.com
  •  * Module :- UnderScore.js Tutorial
  •  * Date Created :- Jan 2014
  •  */
  • var contactList = {
  • "returnList" : [{
  • "name" : "NAME",
  • "password" : "dummy",
  • "time" : "today",
  • "email" : "a.com"

  • }],

  • };

  • window.logger = function(param) {
  • return console.log(param);

  • };

  • 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 = [{

  • "name" : "piyush",
  • "email" : "demo@gmail.com",
  • "age" : 25
  • }, {
  • "name" : "piyush",
  • "email" : "demo@yahoo.com",
  • "age" : 26
  • }, {
  • "name" : "arya",
  • "email" : "demo@hotmail.com",
  • "age" : 27


  • }];

  • /* ************* ARRAY FUNCTION EXAMPLES ********************  */


    /* ****  UNCOMMENT LOGGER TO SEE THE RESULT IN CONSOLE*****  */


    /* ------------------first Function Starts------------------------------------ */

    var firstDemo = _.first(arrColl);
    //var firstDemo = _.first(arrColl,3);
    // 1 2 3 when passed 3 as parameter
    //logger("firstDemo --> " + firstDemo);
    // Returns the first element of an array

    /* -----------------------first Function end--------------------------- */


    /* -----------------------initial Function Starts------------------------ */

    var initialDemo = _.initial(arrColl);
    //var initialDemo = _.first(arrColl,2);
    // 1 2 when pass 2 as parameter
    //logger("initialDemo --> " + initialDemo);
    // Returns everything but the last entry of the array

    /* -------------------------initial Function end------------------------- */


    /* -------------------------last Function Starts------------------------ */

    var lastDemo = _.last(arrColl);
    //var lastDemo = _.last(arrColl,3);
    // 3 4 5 when pass 3 as parameter
    //logger("lastDemo --> " + lastDemo);
    // Returns the last element of an array

    /* --------------------------last Function end------------------------ */


    /* --------------------------rest Function Starts------------------------ */

    //var restDemo = _.rest(arrColl);
    var restDemo = _.rest(arrColl, 3);
    // 4 5 when pass 3 as parameter
    //logger("restDemo --> " + restDemo);
    // Returns the rest of the elements in an array

    /* ---------------------------rest Function end------------------------------- */


    /* ---------------------------compact Function Starts----------------------- */

    var compactDemo = _.compact([0, 1, false, 2, '', 3, null, undefined, '1', '2', 4]);
    //logger("compactDemo --> " + compactDemo);
    // Returns a copy of the array with all falsy values removed

    /* -----------------------compact Function end------------------------------ */


    /* ----------------------flatten Function Starts------------------------------- */

    var flattenDemo = _.flatten([1, [2], [3, [[4]]]], true);
    //logger("flattenDemo --> " + flattenDemo);
    // Flattens a nested array (the nesting can be to any depth).

    /* -----------------------flatten Function end------------------------------- */


    /* ----------------------without Function Starts------------------------- */

    var withoutDemo = _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
    //logger("withoutDemo --> " + withoutDemo);
    // Returns a copy of the array with all instances of the values removed.

    /* ------------------------without Function end------------------------------- */



    /* -------------------------union Function Starts------------------------------- */

    var unionDemo = _.union([1, 2, 3], [101, 2, 1, 10], [2, 1, 9]);
    //logger("unionDemo --> " + unionDemo);
    // Computes the union of the passed-in arrays: the list of unique items.

    /* -------------------------union Function end---------------------------------- */


    /* ------------------------intersection Function Starts--------------------------- */

    var intersectionDemo = _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
    //logger("intersectionDemo --> " + intersectionDemo);
    // Computes the list of values that are the intersection of all the arrays

    /* --------------------------intersection Function end---------------------------- */

    /* -------------------------difference Function Starts---------------------------- */

    var differenceDemo = _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
    //logger("differenceDemo --> " + differenceDemo);
    // Similar to without, but returns the values from array that are not present in the other arrays.

    /* -----------------------difference Function end--------------------------------- */


    /* ----------------------uniq Function Starts--------------------------------------- */

    var uniqDemo = _.uniq([1, 2, 1, 4, 1, 3]);
    //Produces a duplicate-free version of the array, using === to test object equality.
    //logger("uniqDemo --> " + uniqDemo);

    /* ---------------------uniq Function end--------------------------------------------- */

    /* ----------------------zip Function Starts-------------------------------------------- */

    var zipDemo = _.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
    //Merges together the values of each of the arrays with the values at the corresponding position.
    //logger("zipDemo --> " + zipDemo);

    /* ------------------------zip Function end-------------------------------------------- */

    /* ------------------------object Function Starts-------------------------------------- */

    var objectDemo = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
    //var objectDemo = _.object([['moe', 30], ['larry', 40], ['curly', 50]]);
    //Converts arrays into objects. If duplicate keys exist, the last value wins.
    //logger("objectDemo --> " + JSON.stringify(objectDemo));

    /* -------------------------object Function end---------------------------------------- */


    /* ------------------------- -indexOfDemo Function Starts-------------------------- */
    var indexOfDemo = _.indexOf([1, 2, 3], 2);
    //Returns the index at which value can be found in the array, or -1 if value is not present in the array
    // Starts with 0
    //logger("indexOfDemo --> " + indexOfDemo);

    /* ---------------------------indexOfDemo Function end----------------------------- */


    /* --------------------------lastIndexOfDemo Function Starts------------------------- */

    var lastIndexOfDemo = _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
    // starts with 0
    //Returns the index of the last occurrence of value in the array, or -1 if value is not present.
    //logger("lastIndexOfDemo --> " + lastIndexOfDemo);

    /* --------------------------lastIndexOfDemo Function end----------------------------- */


    /* -------------------------sortedIndex Function Starts---------------------------------- */

    var sortedIndexDemo = _.sortedIndex([10, 20, 30, 40, 50], 55);
    // starts with 0
    //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
    //logger("sortedIndexDemo --> " + sortedIndexDemo);

    /* --------------------------sortedIndex Function end------------------------------------ */

    /* ---------------------------range Function Starts---------------------------------------- */

    var rangeDemo = _.range(10);
    //var rangeDemo = _.range(1, 11);
    //var rangeDemo =_.range(0, 30, 5);
    //var rangeDemo =_.range(0, -10, -1);
    //var rangeDemo =_.range(0);
    //logger("rangeDemo --> " + rangeDemo);

    /* --------------------------range Function end------------------------------------------ */

    Click Here For Underscore Collections Functions with Examples