- /**
- * Author Benson Jala
- * Email : jalabenson@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" : "benson",
- "email" : "demo@gmail.com",
- "age" : 25
- }, {
- "name" : "benson",
- "email" : "demo@yahoo.com",
- "age" : 26
- }, {
- "name" : "jala",
- "email" : "demo@hotmail.com",
- "age" : 27
- }];
- /* *************** COLLECTIONS EXAMPLES START********************* */
- /* **** UNCOMMENT LOGGER TO SEE THE RESULT IN CONSOLE***** */
- /* ---------------Each Function Start---------------------------- */
- var eachDemo = _.each(arr, function(value, key) {
- //logger("each values -->"+value);
- //logger("each Key[Index]-->"+key);
- return value;
- });
- //logger("eachDemo -->"+eachDemo); //Will be undefined as not stored in array
- /* --------------Each Function End-------------------------------- */
- /* --------------Map Function Start------------------------------- */
- var mapDemo = _.map(arr, function(value, key) {
- //logger("map values -->"+value);
- //logger("map Key[Index]-->"+key);
- return value;
- });
- //logger("mapDemo -->"+ mapDemo); //Map stores value/key in an array. Will show the result in array
- /* -------------------------Map Function End--------------------- */
- /* -----------------------Reduce Function Starts------------------- */
- var reduceDemo = _.reduce(arr, function(a, b) {
- //logger("a is -- > "+a ); // values adds on increment with iterator 1 2 3 4
- //logger("b is --> "+b); // iterator/ increment
- return (a + " " + b);
- // Starts from Left
- });
- //logger("reduceDemo --> " + reduceDemo);
- // whole value
- /* ----------------------Reduce Function End---------------------- */
- /* ----------------------ReduceRight Function Starts--------------- */
- var reduceRightDemo = _.reduceRight(arr, function(a, b) {
- //logger("a is -- > "+a ); // values adds on increment with iterator 4 3 2 1
- //logger("b is --> "+b); // iterator/ increment
- return (a + " " + b);
- // Starts from Right
- });
- //logger("reduceRightDemo --> " + reduceRightDemo);
- // whole value
- /* ------------------ReduceRight Function End--------------------- */
- /* -----------------Find Function Starts-------------------------------- */
- var findDemo = _.find(arr, function(a) {
- return (a % 2 == 0);
- });
- //logger("findDemo --> " + findDemo);
- // it returns first item
- /* ----------Find Function end-------------------------------- */
- /* ---------Filter Function Starts------------------------------ */
- var filterDemo = _.filter(arr, function(a) {
- return (a % 2 == 0);
- });
- //logger("filterDemo --> " + filterDemo);
- // It returns an array .It gives even
- /* -------Filter Function end-------------------- */
- /* ---------Where Function Starts---------------- */
- var whereDemo = _.where(json, {
- name : "benson"
- });
- //logger("whereDemo --> " + JSON.stringify(whereDemo));
- // It returns an array in key and value pair
- /* ---------Where Function end------------------- */
- /* -------findWhere Function Starts---------------- */
- var findWhereDemo = _.findWhere(json, {
- name : "benson"
- });
- //logger("findWhereDemo --> " + JSON.stringify(findWhereDemo));
- // It returns 1st key and value pair in array
- /* -------findWhere Function end---------------------- */
- /* ----------------reject Function Starts------------------ */
- var rejectDemo = _.reject(arr, function(a) {
- return (a % 2 == 0);
- });
- //logger("rejectDemo --> " + rejectDemo);
- // opposite of filter. It returns an array . It gives odd
- /* ----------reject Function end--------------------------- */
- /* --------every Function Starts----------------------- */
- var everyDemo = _.every(every);
- //logger("everyDemo --> " + everyDemo);
- // false, undefined, null, 0 will give ---> false
- /* --------every Function end----------------------- */
- /* -------some Function Starts--------------------- */
- var someDemo = _.some(every);
- //logger("someDemo --> " + someDemo);
- // opposite of every
- // false, undefined, null, 0 will give ---> true
- /* -------some Function end--------------------- */
- /* -------contains Function Starts---------------- */
- var containsDemo = _.contains(every, 0);
- //logger("containsDemo --> " + containsDemo);
- // It is case sensitive "HELLO" will not work
- // It is used to see whether the values is available in the list or not
- // takes only one parameter _.contains(every,0,"hi");
- /* --------contains Function end------------------------ */
- /* ------invoke Function Starts---------------------------- */
- var invokeDemo = _.invoke(invokeSort, 'sort');
- //var invokeDemo = _.invoke(invokeupper, 'toUpperCase');
- //var mapInvokeDemo = _.map(json, function(param){return param.email;});
- //var pluckInvokeDemo = _.pluck(json, 'email');
- //var invokeDemo = _.invoke(pluckInvokeDemo, 'toUpperCase');
- //var invokeDemo = _.invoke(mapInvokeDemo, 'toUpperCase');
- //logger("invokeDemo --> " + invokeDemo);
- // sort takes list
- // uppercase takes single array
- /* -------invoke Function end---------------------- */
- /* -----pluck Function Starts-------------------------- */
- var pluckDemo = _.pluck(json, 'email');
- //logger("pluckDemo --> " + pluckDemo);
- //extracting a list of property values.
- /* -----------------pluck Function end---------------------------------------------------- */
- /* ----------------max Function Starts---------------------------------------------------- */
- var maxDemo = _.max(json, function(param) {
- return param.age;
- });
- //logger("maxDemo --> " + JSON.stringify(maxDemo));
- // gives the array with max value
- // if max value it will give first value which is iterated
- /* --------------max Function end---------------------------------------------------- */
- /* ---------------min Function Starts---------------------------------------------------- */
- var minDemo = _.min(json, function(param) {
- return param.age;
- });
- //logger("minDemo --> " + JSON.stringify(minDemo));
- // gives the array with min value
- /* ---------------max Function end---------------------------------------------------- */
- /* ---------------sortBy Function Starts------------------------------------ */
- var sortByDemo = _.sortBy(json, function(param) {
- return param.age;
- });
- //logger("sortByDemo --> " + JSON.stringify(sortByDemo));
- // gives the array with sorted value
- /* -----------------sortBy Function end---------------------------------------------------- */
- /* -----------------groupBy Function Starts------------------------------------- */
- var groupByDemo = _.groupBy(json, function(param) {
- return param.name;
- //return param.age;
- });
- //logger("groupByDemo --> " + JSON.stringify(groupByDemo));
- // divides into group and gives the value
- /* -----------------groupBy Function end---------------------------------------------------- */
- /* ----------------indexBy Function Starts----------------------------------------- */
- var indexByDemo = _.indexBy(json, 'age');
- //logger("indexByDemo --> " + JSON.stringify(indexByDemo));
- // segregate according to the age
- /* ---------------indexBy Function end-------------------------------------- */
- /* ---------------countBy Function Starts-------------------------- */
- var countByDemo = _.countBy(json, 'name');
- //logger("countByDemo --> " + JSON.stringify(countByDemo));
- // gives count
- /* ---------------countBy Function end---------------------------- */
- /* -----------------sample Function Starts---------------------------- */
- var sampleDemo = _.sample(arr, '3');
- //logger("sampleDemo --> " + sampleDemo);
- // gives random result
- /* ------------------sample Function end---------------------- */
- /* -------------------toArray Function Starts------------------------------ */
- var toArrayDemo = _.toArray(arr);
- //logger("toArrayDemo --> " + toArrayDemo);
- // convert into array
- /* --------------------toArray Function end---------------------- */
- /* ---------------------size Function Starts-------------------------- */
- var sizeDemo = _.size(json);
- //logger("sizeDemo --> " + sizeDemo);
- // gives the size of the array
- /* ------------------------size Function end-------------------- */
- /* *************** COLLECTIONS EXAMPLES END************** */