Created
March 5, 2016 21:09
-
-
Save jrjames83/5dec8f9f897c12479e82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var data = [ | |
| { | |
| "_id": "56db20d2d9944c0932ecd1c8", | |
| "index": 0, | |
| "guid": "e054234d-c7ab-4e50-bf5b-b30dc93397ab", | |
| "isActive": false, | |
| "balance": "$1,172.10", | |
| "picture": "http://placehold.it/32x32", | |
| "age": 27, | |
| "eyeColor": "green", | |
| "name": "Julia Nichols", | |
| "gender": "female", | |
| "company": "TUBALUM", | |
| "email": "[email protected]", | |
| "phone": "+1 (982) 404-3852", | |
| "address": "498 Bush Street, Goochland, Federated States Of Micronesia, 6754", | |
| "about": "Anim consectetur labore dolor proident minim enim pariatur. Est pariatur voluptate Lorem excepteur amet eiusmod. Officia amet reprehenderit id eu. Officia nisi sint cupidatat deserunt. Mollit ipsum aute ut aliqua minim nisi velit mollit. Mollit velit eu commodo consequat esse ullamco commodo reprehenderit et. Cupidatat deserunt adipisicing sit minim ullamco quis pariatur ea qui ullamco.\r\n", | |
| "registered": "2014-10-11T03:24:34 +06:00", | |
| "latitude": 55.229875, | |
| "longitude": 71.584897, | |
| "tags": [ | |
| "laborum", | |
| "incididunt", | |
| "adipisicing", | |
| "reprehenderit", | |
| "excepteur", | |
| "occaecat", | |
| "id" | |
| ], | |
| "friends": [ | |
| { | |
| "id": 0, | |
| "name": "Jacqueline Goff" | |
| }, | |
| { | |
| "id": 1, | |
| "name": "Chrystal Patrick" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Perez Ruiz" | |
| } | |
| ], | |
| "greeting": "Hello, Julia Nichols! You have 10 unread messages.", | |
| "favoriteFruit": "strawberry" | |
| }, | |
| { | |
| "_id": "56db20d22e3086ae0128f823", | |
| "index": 1, | |
| "guid": "47222610-b588-495f-b333-67d42671b12e", | |
| "isActive": true, | |
| "balance": "$1,301.98", | |
| "picture": "http://placehold.it/32x32", | |
| "age": 21, | |
| "eyeColor": "blue", | |
| "name": "Eugenia Chaney", | |
| "gender": "female", | |
| "company": "ZENTILITY", | |
| "email": "[email protected]", | |
| "phone": "+1 (916) 405-2720", | |
| "address": "996 Highland Place, Abiquiu, Montana, 1457", | |
| "about": "Exercitation do Lorem ipsum sunt non pariatur ad dolor dolore voluptate. Sunt dolore ut qui voluptate anim non quis irure. Voluptate aliquip in dolor culpa nisi est magna esse ea cillum occaecat Lorem Lorem consectetur. Aliquip et ex pariatur qui non qui ut velit veniam. Lorem Lorem elit quis fugiat magna reprehenderit tempor esse exercitation duis do ut incididunt. Cillum occaecat fugiat enim consectetur aute incididunt adipisicing deserunt culpa quis. Irure qui adipisicing id voluptate voluptate.\r\n", | |
| "registered": "2014-04-08T02:13:29 +06:00", | |
| "latitude": 30.279567, | |
| "longitude": -84.53015, | |
| "tags": [ | |
| "velit", | |
| "non", | |
| "excepteur", | |
| "dolor", | |
| "sint", | |
| "sit", | |
| "Lorem" | |
| ], | |
| "friends": [ | |
| { | |
| "id": 0, | |
| "name": "Dejesus Butler" | |
| }, | |
| { | |
| "id": 1, | |
| "name": "Nichols Beck" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Nanette Talley" | |
| } | |
| ], | |
| "greeting": "Hello, Eugenia Chaney! You have 4 unread messages.", | |
| "favoriteFruit": "banana" | |
| }, | |
| { | |
| "_id": "56db20d218ad801b6afb3d1e", | |
| "index": 2, | |
| "guid": "044a5c34-17d5-44af-aa0c-40ff9c0c7e9c", | |
| "isActive": false, | |
| "balance": "$1,719.10", | |
| "picture": "http://placehold.it/32x32", | |
| "age": 23, | |
| "eyeColor": "green", | |
| "name": "Claudine Huffman", | |
| "gender": "female", | |
| "company": "TRI@TRIBALOG", | |
| "email": "claudinehuffman@[email protected]", | |
| "phone": "+1 (905) 562-3558", | |
| "address": "819 Varick Street, Dupuyer, Pennsylvania, 2483", | |
| "about": "Id veniam non deserunt incididunt occaecat laboris consectetur consequat aliquip voluptate non enim ex. Nostrud enim laborum laborum exercitation duis duis id enim nostrud laboris excepteur nisi voluptate reprehenderit. Ea officia dolor do proident quis id incididunt non excepteur anim pariatur reprehenderit esse. Et quis Lorem ad proident quis voluptate dolore Lorem ut. Consectetur cupidatat ea ut qui consectetur ipsum magna proident ex aute consequat.\r\n", | |
| "registered": "2014-12-03T11:27:31 +07:00", | |
| "latitude": -36.21535, | |
| "longitude": 157.519234, | |
| "tags": [ | |
| "veniam", | |
| "consectetur", | |
| "aliqua", | |
| "commodo", | |
| "sint", | |
| "nisi", | |
| "nulla" | |
| ], | |
| "friends": [ | |
| { | |
| "id": 0, | |
| "name": "Alexander Ratliff" | |
| }, | |
| { | |
| "id": 1, | |
| "name": "Twila Barlow" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Shepherd Quinn" | |
| } | |
| ], | |
| "greeting": "Hello, Claudine Huffman! You have 9 unread messages.", | |
| "favoriteFruit": "apple" | |
| }, | |
| { | |
| "_id": "56db20d23fc03315b0fde298", | |
| "index": 3, | |
| "guid": "69a0b8ee-5ad8-4b5f-9074-70a2bc945613", | |
| "isActive": true, | |
| "balance": "$1,609.32", | |
| "picture": "http://placehold.it/32x32", | |
| "age": 28, | |
| "eyeColor": "green", | |
| "name": "Bobbie Sosa", | |
| "gender": "female", | |
| "company": "MANGLO", | |
| "email": "[email protected]", | |
| "phone": "+1 (930) 461-3961", | |
| "address": "687 Durland Place, Websterville, Utah, 5617", | |
| "about": "Culpa fugiat ut ipsum eu et. Consectetur labore enim sunt anim magna non cillum. Cillum officia tempor dolor eu voluptate incididunt ad ipsum amet ex. Commodo commodo ut incididunt nostrud sunt mollit nostrud eu ut. Aliquip amet amet consequat sint et excepteur labore deserunt enim consequat eiusmod. Esse cillum cillum est et cillum occaecat ex cillum exercitation occaecat velit. Sit tempor commodo sint aliqua adipisicing elit eu cillum.\r\n", | |
| "registered": "2015-10-12T12:45:00 +06:00", | |
| "latitude": -66.397037, | |
| "longitude": -8.113268, | |
| "tags": [ | |
| "incididunt", | |
| "consequat", | |
| "mollit", | |
| "occaecat", | |
| "et", | |
| "qui", | |
| "consequat" | |
| ], | |
| "friends": [ | |
| { | |
| "id": 0, | |
| "name": "Lakeisha Mack" | |
| }, | |
| { | |
| "id": 1, | |
| "name": "Macias Hensley" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Ashley Hinton" | |
| } | |
| ], | |
| "greeting": "Hello, Bobbie Sosa! You have 7 unread messages.", | |
| "favoriteFruit": "strawberry" | |
| }, | |
| { | |
| "_id": "56db20d2c0443939a29f8d4a", | |
| "index": 4, | |
| "guid": "9ce16e0f-08f1-4813-b49e-3c96807c71da", | |
| "isActive": false, | |
| "balance": "$2,591.87", | |
| "picture": "http://placehold.it/32x32", | |
| "age": 22, | |
| "eyeColor": "blue", | |
| "name": "Carney Soto", | |
| "gender": "male", | |
| "company": "CALCU", | |
| "email": "[email protected]", | |
| "phone": "+1 (865) 404-2348", | |
| "address": "860 School Lane, Thynedale, Vermont, 1439", | |
| "about": "Fugiat amet minim aliquip cupidatat sint ea mollit. Est eu pariatur officia exercitation minim reprehenderit consectetur esse amet do eiusmod dolor et. Tempor cillum eiusmod consequat cillum tempor quis ad tempor aliqua sit consectetur mollit velit. Sunt fugiat aliquip proident do voluptate esse minim commodo et elit.\r\n", | |
| "registered": "2016-01-11T09:30:13 +07:00", | |
| "latitude": -84.048812, | |
| "longitude": -135.590423, | |
| "tags": [ | |
| "eiusmod", | |
| "aute", | |
| "ut", | |
| "ipsum", | |
| "ex", | |
| "adipisicing", | |
| "culpa" | |
| ], | |
| "friends": [ | |
| { | |
| "id": 0, | |
| "name": "Lina Phelps" | |
| }, | |
| { | |
| "id": 1, | |
| "name": "Smith Moran" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "Yvette Webb" | |
| } | |
| ], | |
| "greeting": "Hello, Carney Soto! You have 9 unread messages.", | |
| "favoriteFruit": "strawberry" | |
| } | |
| ]; | |
| // Get the sum of the ages | |
| var age = 0; | |
| for(obj in data) { | |
| age += data[obj].age; | |
| } | |
| console.log(age, " Is the for loop"); | |
| // try via map reduce | |
| var testy = | |
| data.map(function(obj) { | |
| return obj.age; | |
| }).reduce(function(x,y) { | |
| return x + y; | |
| }); | |
| console.log(testy, "is the map reduce one") | |
| // instructor suggestions | |
| var agesum = data.reduce(function(accum, item, index) { | |
| accum += item.age; | |
| return accum; | |
| }, 0); | |
| console.log(agesum, " is the age sum"); | |
| // Create new object that's key'd by email and has the name | |
| var newobject = data.reduce(function(accum, item, index) { | |
| accum[item.email] = item.name | |
| return accum; | |
| }, {}) | |
| console.log(newobject); | |
| // You always have to return all but you need not return the orignal number of elements | |
| // as you would with a call to map | |
| var fem_object = data.reduce(function(accum, item, index) { | |
| if(item.gender === "Female") { | |
| accum[item.email] = item.name; | |
| } | |
| return accum; | |
| }, {}) | |
| console.log(fem_object, "are the female ones") | |
| // reorganize data using an initial object with other keys | |
| var more = data.reduce(function(accum, item, index) { | |
| if(item.gender === "female") { | |
| accum.female.push(item.name); // OR accum[item.gender] = item for the entire item | |
| } else { | |
| accum.male.push(item.name); | |
| } return accum; | |
| }, {male: [], female: []}); | |
| console.log(more, " Is my gender lists"); | |
| // Get frequencies by gender | |
| var freq = data.reduce(function(accum, item, index) { | |
| if(accum[item.gender]) { | |
| accum[item.gender]++; | |
| } else { | |
| accum[item.gender] = 1; | |
| } return accum; | |
| }, {}) | |
| console.log(freq, " are my frequencies") | |
| // writing reduce once again for the fun of it - this time on the prototype | |
| Array.prototype.reeeduce = function(fn, start) { | |
| var current = start; | |
| for(var i = 0; i < this.length; i++) { | |
| current = fn(current, this[i]); | |
| } | |
| return current; | |
| } | |
| // use my reduce function to....multiple all ages! | |
| var age_mults = data.reeeduce(function(accum, item) { | |
| return accum * item.age; | |
| },1); | |
| console.log(age_mults, "are my age mults") | |
| // Get a list of balances, convert to number and sum them all | |
| var bals = data.map(function(val) { | |
| return val.balance; | |
| }).map(function(bal) { | |
| return parseFloat(bal.replace(",", "").replace("\$", "")); | |
| }).reeeduce(function(accum, item) { | |
| return accum + item; | |
| },0); | |
| console.log(bals); | |
| // Get all balances using ONLY reduce | |
| var bals2 = data.reduce(function(accum, item, index) { | |
| accum[item._id] = item.balance; | |
| //accum.push(item.balance); | |
| return accum; | |
| }, {}); | |
| console.log(bals2); | |
| var bals3 = data.reeeduce (function(accum, item, index) { | |
| accum[item._id] = item.balance; | |
| //accum.push(item.balance); | |
| return accum; | |
| }, {}); | |
| console.log(bals3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment