UnderScore.js Tutorial [Collections Functions with Examples]


  1. /**
  2.  * Author Benson Jala
  3. * Email : jalabenson@gmail.com
  4.  * Module :- UnderScore.js Tutorial
  5.  * Date Created :- Jan 2014
  6.  */
  7. var contactList = {
  8. "returnList" : [{
  9. "name" : "NAME",
  10. "password" : "dummy",
  11. "time" : "today",
  12. "email" : "a.com"

  13. }],

  14. };

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

  17. };

  18. 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 = [{

  19. "name" : "benson",
  20. "email" : "demo@gmail.com",
  21. "age" : 25
  22. }, {
  23. "name" : "benson",
  24. "email" : "demo@yahoo.com",
  25. "age" : 26
  26. }, {
  27. "name" : "jala",
  28. "email" : "demo@hotmail.com",
  29. "age" : 27
  30. }];

  31. /* ***************  COLLECTIONS EXAMPLES START*********************  */

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

  1. /* ---------------Each Function Start---------------------------- */

  2. var eachDemo = _.each(arr, function(value, key) {
  3. //logger("each values -->"+value);
  4. //logger("each Key[Index]-->"+key);
  5. return value;
  6. });
  7. //logger("eachDemo -->"+eachDemo);  //Will be undefined as not stored in array

  8. /* --------------Each Function End-------------------------------- */

  1. /* --------------Map Function Start------------------------------- */

  2. var mapDemo = _.map(arr, function(value, key) {
  3. //logger("map values -->"+value);
  4. //logger("map Key[Index]-->"+key);
  5. return value;
  6. });
  7. //logger("mapDemo -->"+ mapDemo); //Map stores value/key in an array. Will show the result in array

  8. /* -------------------------Map Function End--------------------- */

  1. /* -----------------------Reduce Function Starts------------------- */

  2. var reduceDemo = _.reduce(arr, function(a, b) {
  3. //logger("a is -- > "+a ); // values adds on increment with iterator 1 2 3 4
  4. //logger("b is  --> "+b); // iterator/ increment
  5. return (a + " " + b);

  6. // Starts from Left

  7. });
  8. //logger("reduceDemo --> " + reduceDemo);
  9. // whole value

  10. /* ----------------------Reduce Function End---------------------- */

  1. /* ----------------------ReduceRight Function Starts--------------- */

  2. var reduceRightDemo = _.reduceRight(arr, function(a, b) {
  3. //logger("a is -- > "+a ); // values adds on increment with iterator 4 3 2 1
  4. //logger("b is  --> "+b); // iterator/ increment
  5. return (a + " " + b);

  6. // Starts from Right

  7. });
  8. //logger("reduceRightDemo --> " + reduceRightDemo);
  9. // whole value

  10. /* ------------------ReduceRight Function End--------------------- */

  1. /* -----------------Find Function Starts-------------------------------- */

  2. var findDemo = _.find(arr, function(a) {
  3. return (a % 2 == 0);
  4. });
  5. //logger("findDemo --> " + findDemo);
  6. // it returns first item

  7. /* ----------Find Function end-------------------------------- */

  1. /* ---------Filter Function Starts------------------------------ */

  2. var filterDemo = _.filter(arr, function(a) {
  3. return (a % 2 == 0);
  4. });
  5. //logger("filterDemo --> " + filterDemo);
  6. // It returns an array .It gives even

  7. /* -------Filter Function end-------------------- */

  1. /* ---------Where Function Starts---------------- */

  2. var whereDemo = _.where(json, {
  3. name : "benson"
  4. });
  5. //logger("whereDemo --> " + JSON.stringify(whereDemo));
  6. // It returns an array in key and value pair
  7. /* ---------Where Function end------------------- */


  1. /* -------findWhere Function Starts---------------- */

  2. var findWhereDemo = _.findWhere(json, {
  3. name : "benson"
  4. });
  5. //logger("findWhereDemo --> " + JSON.stringify(findWhereDemo));
  6. // It returns 1st key and value pair in array

  7. /* -------findWhere Function end---------------------- */

  1. /* ----------------reject Function Starts------------------ */

  2. var rejectDemo = _.reject(arr, function(a) {
  3. return (a % 2 == 0);
  4. });
  5. //logger("rejectDemo --> " + rejectDemo);
  6. // opposite of filter. It returns an array . It gives odd

  7. /* ----------reject Function end--------------------------- */

  1. /* --------every Function Starts----------------------- */

  2. var everyDemo = _.every(every);
  3. //logger("everyDemo --> " + everyDemo);
  4. // false, undefined, null, 0  will give ---> false

  5. /* --------every Function end----------------------- */

  1. /* -------some Function Starts--------------------- */

  2. var someDemo = _.some(every);
  3. //logger("someDemo --> " + someDemo);
  4. // opposite of every
  5. // false, undefined, null, 0  will give ---> true
  6. /* -------some Function end--------------------- */


  1. /* -------contains Function Starts---------------- */

  2. var containsDemo = _.contains(every, 0);
  3. //logger("containsDemo --> " + containsDemo);
  4. // It is case sensitive "HELLO" will not work
  5. // It is used to see whether the values is available in the list or not
  6. // takes only one parameter  _.contains(every,0,"hi");

  7. /* --------contains Function end------------------------ */

  1. /* ------invoke Function Starts---------------------------- */

  2. var invokeDemo = _.invoke(invokeSort, 'sort');
  3. //var invokeDemo = _.invoke(invokeupper, 'toUpperCase');
  4. //var mapInvokeDemo = _.map(json, function(param){return param.email;});
  5. //var pluckInvokeDemo = _.pluck(json, 'email');
  6. //var invokeDemo = _.invoke(pluckInvokeDemo, 'toUpperCase');
  7. //var invokeDemo = _.invoke(mapInvokeDemo, 'toUpperCase');
  8. //logger("invokeDemo --> " + invokeDemo);
  9. // sort takes list
  10. // uppercase takes single array

  11. /* -------invoke Function end---------------------- */

  1. /* -----pluck Function Starts-------------------------- */

  2. var pluckDemo = _.pluck(json, 'email');
  3. //logger("pluckDemo --> " + pluckDemo);
  4. //extracting a list of property values.
  5. /* -----------------pluck Function end---------------------------------------------------- */

  1. /* ----------------max Function Starts---------------------------------------------------- */

  2. var maxDemo = _.max(json, function(param) {
  3. return param.age;
  4. });
  5. //logger("maxDemo --> " + JSON.stringify(maxDemo));
  6. // gives the array with max value
  7. // if max value it will give first value which is iterated

  8. /* --------------max Function end---------------------------------------------------- */

  1. /* ---------------min Function Starts---------------------------------------------------- */

  2. var minDemo = _.min(json, function(param) {
  3. return param.age;
  4. });
  5. //logger("minDemo --> " + JSON.stringify(minDemo));
  6. // gives the array with min value

  7. /* ---------------max Function end---------------------------------------------------- */

  1. /* ---------------sortBy Function Starts------------------------------------ */

  2. var sortByDemo = _.sortBy(json, function(param) {
  3. return param.age;
  4. });
  5. //logger("sortByDemo --> " + JSON.stringify(sortByDemo));
  6. // gives the array with sorted  value

  7. /* -----------------sortBy Function end---------------------------------------------------- */

  1. /* -----------------groupBy Function Starts------------------------------------- */

  2. var groupByDemo = _.groupBy(json, function(param) {
  3. return param.name;
  4. //return param.age;
  5. });
  6. //logger("groupByDemo --> " + JSON.stringify(groupByDemo));
  7. // divides into group and gives the value
  8. /* -----------------groupBy Function end---------------------------------------------------- */

  9. /* ----------------indexBy Function Starts----------------------------------------- */

  10. var indexByDemo = _.indexBy(json, 'age');
  11. //logger("indexByDemo --> " + JSON.stringify(indexByDemo));
  12. // segregate according to the age

  13. /* ---------------indexBy Function end-------------------------------------- */

  1. /* ---------------countBy Function Starts-------------------------- */

  2. var countByDemo = _.countBy(json, 'name');
  3. //logger("countByDemo --> " + JSON.stringify(countByDemo));
  4. // gives count

  5. /* ---------------countBy Function end---------------------------- */

  1. /* -----------------sample Function Starts---------------------------- */

  2. var sampleDemo = _.sample(arr, '3');
  3. //logger("sampleDemo --> " + sampleDemo);
  4. // gives random result

  5. /* ------------------sample Function end---------------------- */

  1. /* -------------------toArray Function Starts------------------------------ */

  2. var toArrayDemo = _.toArray(arr);
  3. //logger("toArrayDemo --> " + toArrayDemo);
  4. // convert into array

  5. /* --------------------toArray Function end---------------------- */

  1. /* ---------------------size Function Starts-------------------------- */
  2. var sizeDemo = _.size(json);
  3. //logger("sizeDemo --> " + sizeDemo);
  4. // gives the size of the array
  5. /* ------------------------size Function end-------------------- */



  6. /* ***************  COLLECTIONS EXAMPLES END**************  */