Created
June 4, 2015 20:04
-
-
Save johnkawakami/e94bd21d774c482a86e4 to your computer and use it in GitHub Desktop.
jQuery, lodash, senators, simple example
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
| <html> | |
| <head> | |
| <script src="lodash.js"></script> | |
| <script src="jquery-2.1.4.js"></script> | |
| </head> | |
| <body> | |
| <p> | |
| California's delegation: <span id="cal"></span> | |
| </p> | |
| <p> | |
| Average age in the Senate: <span id="average"></span> | |
| </p> | |
| <script> | |
| var a = [ | |
| { "name":"Dianne Feinstein", "state":"California", "birthdate":"06/22/33" }, | |
| { "name":"Chuck Grassley", "state":"Iowa", "birthdate":"09/17/33" }, | |
| { "name":"Orrin Hatch", "state":"Utah", "birthdate":"03/22/34" }, | |
| { "name":"Richard Shelby", "state":"Alabama", "birthdate":"05/06/34" }, | |
| { "name":"Jim Inhofe", "state":"Oklahoma", "birthdate":"11/17/34" }, | |
| { "name":"Pat Roberts", "state":"Kansas", "birthdate":"04/20/36" }, | |
| { "name":"Barbara Mikulski", "state":"Maryland", "birthdate":"07/20/36" }, | |
| { "name":"John McCain", "state":"Arizona", "birthdate":"08/29/36" }, | |
| { "name":"Thad Cochran", "state":"Mississippi", "birthdate":"12/07/37" }, | |
| { "name":"Harry Reid", "state":"Nevada", "birthdate":"12/02/39" }, | |
| { "name":"Patrick Leahy", "state":"Vermont", "birthdate":"03/31/40" }, | |
| { "name":"Lamar Alexander", "state":"Tennessee", "birthdate":"07/03/40" }, | |
| { "name":"Barbara Boxer", "state":"California", "birthdate":"11/11/40" }, | |
| { "name":"Bernie Sanders", "state":"Vermont", "birthdate":"09/08/41" }, | |
| { "name":"Mitch McConnell", "state":"Kentucky", "birthdate":"02/20/42" }, | |
| { "name":"Bill Nelson", "state":"Florida", "birthdate":"09/29/42" }, | |
| { "name":"Jim Risch", "state":"Idaho", "birthdate":"05/03/43" }, | |
| { "name":"Dan Coats", "state":"Indiana", "birthdate":"05/16/43" }, | |
| { "name":"Ben Cardin", "state":"Maryland", "birthdate":"10/05/43" }, | |
| { "name":"Mike Enzi", "state":"Wyoming", "birthdate":"02/01/44" }, | |
| { "name":"Angus King", "state":"Maine", "birthdate":"03/31/44" }, | |
| { "name":"Dick Durbin", "state":"Illinois", "birthdate":"11/21/44" }, | |
| { "name":"Johnny Isakson", "state":"Georgia", "birthdate":"12/28/44" }, | |
| { "name":"Richard Blumenthal", "state":"Connecticut", "birthdate":"02/13/46" }, | |
| { "name":"Ed Markey", "state":"Massachusetts", "birthdate":"07/11/46" }, | |
| { "name":"Jeff Sessions", "state":"Alabama", "birthdate":"12/24/46" }, | |
| { "name":"Tom Carper", "state":"Delaware", "birthdate":"01/23/47" }, | |
| { "name":"Jeanne Shaheen", "state":"New Hampshire", "birthdate":"01/28/47" }, | |
| { "name":"Joe Manchin", "state":"West Virginia", "birthdate":"08/24/47" }, | |
| { "name":"Mazie Hirono", "state":"Hawaii", "birthdate":"11/03/47" }, | |
| { "name":"Tom Udall", "state":"New Mexico", "birthdate":"05/18/48" }, | |
| { "name":"Ron Wyden", "state":"Oregon", "birthdate":"05/03/49" }, | |
| { "name":"Elizabeth Warren", "state":"Massachusetts", "birthdate":"06/22/49" }, | |
| { "name":"Jack Reed", "state":"Rhode Island", "birthdate":"11/12/49" }, | |
| { "name":"David Perdue", "state":"Georgia", "birthdate":"12/10/49" }, | |
| { "name":"Roy Blunt", "state":"Missouri", "birthdate":"01/10/50" }, | |
| { "name":"Debbie Stabenow", "state":"Michigan", "birthdate":"04/29/50" }, | |
| { "name":"Patty Murray", "state":"Washington", "birthdate":"10/11/50" }, | |
| { "name":"Chuck Schumer", "state":"New York", "birthdate":"11/23/50" }, | |
| { "name":"John Boozman", "state":"Arkansas", "birthdate":"12/10/50" }, | |
| { "name":"Deb Fischer", "state":"Nebraska", "birthdate":"03/01/51" }, | |
| { "name":"Mike Crapo", "state":"Idaho", "birthdate":"05/20/51" }, | |
| { "name":"Al Franken", "state":"Minnesota", "birthdate":"05/21/51" }, | |
| { "name":"Roger Wicker", "state":"Mississippi", "birthdate":"07/05/51" }, | |
| { "name":"John Cornyn", "state":"Texas", "birthdate":"02/02/52" }, | |
| { "name":"John Barrasso", "state":"Wyoming", "birthdate":"07/21/52" }, | |
| { "name":"Bob Corker", "state":"Tennessee", "birthdate":"08/24/52" }, | |
| { "name":"Sherrod Brown", "state":"Ohio", "birthdate":"11/09/52" }, | |
| { "name":"Susan Collins", "state":"Maine", "birthdate":"12/07/52" }, | |
| { "name":"Claire McCaskill", "state":"Missouri", "birthdate":"07/24/53" }, | |
| { "name":"Shelley Moore Capito", "state":"West Virginia", "birthdate":"11/26/53" }, | |
| { "name":"Bob Menendez", "state":"New Jersey", "birthdate":"01/01/54" }, | |
| { "name":"Jerry Moran", "state":"Kansas", "birthdate":"05/29/54" }, | |
| { "name":"Mike Rounds", "state":"South Dakota", "birthdate":"10/24/54" }, | |
| { "name":"Mark Warner", "state":"Virginia", "birthdate":"12/15/54" }, | |
| { "name":"Ron Johnson", "state":"Wisconsin", "birthdate":"04/08/55" }, | |
| { "name":"Lindsey Graham", "state":"South Carolina", "birthdate":"07/09/55" }, | |
| { "name":"Joe Donnelly", "state":"Indiana", "birthdate":"09/29/55" }, | |
| { "name":"Sheldon Whitehouse", "state":"Rhode Island", "birthdate":"10/20/55" }, | |
| { "name":"Heidi Heitkamp", "state":"North Dakota", "birthdate":"10/30/55" }, | |
| { "name":"Richard Burr", "state":"North Carolina", "birthdate":"11/30/55" }, | |
| { "name":"Rob Portman", "state":"Ohio", "birthdate":"12/19/55" }, | |
| { "name":"Jon Tester", "state":"Montana", "birthdate":"08/21/56" }, | |
| { "name":"Jeff Merkley", "state":"Oregon", "birthdate":"10/24/56" }, | |
| { "name":"John Hoeven", "state":"North Dakota", "birthdate":"03/13/57" }, | |
| { "name":"Lisa Murkowski", "state":"Alaska", "birthdate":"05/22/57" }, | |
| { "name":"Bill Cassidy", "state":"Louisiana", "birthdate":"09/28/57" }, | |
| { "name":"Tim Kaine", "state":"Virginia", "birthdate":"02/26/58" }, | |
| { "name":"Maria Cantwell", "state":"Washington", "birthdate":"10/13/58" }, | |
| { "name":"Gary Peters", "state":"Michigan", "birthdate":"12/01/58" }, | |
| { "name":"Mark Kirk", "state":"Illinois", "birthdate":"09/15/59" }, | |
| { "name":"Bob Casey, Jr.", "state":"Pennsylvania", "birthdate":"04/13/60" }, | |
| { "name":"Dean Heller", "state":"Nevada", "birthdate":"05/10/60" }, | |
| { "name":"Amy Klobuchar", "state":"Minnesota", "birthdate":"05/25/60" }, | |
| { "name":"Thom Tillis", "state":"North Carolina", "birthdate":"08/30/60" }, | |
| { "name":"John Thune", "state":"South Dakota", "birthdate":"01/07/61" }, | |
| { "name":"David Vitter", "state":"Louisiana", "birthdate":"05/03/61" }, | |
| { "name":"Pat Toomey", "state":"Pennsylvania", "birthdate":"11/17/61" }, | |
| { "name":"Tammy Baldwin", "state":"Wisconsin", "birthdate":"02/11/62" }, | |
| { "name":"Steve Daines", "state":"Montana", "birthdate":"08/20/62" }, | |
| { "name":"Jeff Flake", "state":"Arizona", "birthdate":"12/31/62" }, | |
| { "name":"Rand Paul", "state":"Kentucky", "birthdate":"01/07/63" }, | |
| { "name":"Chris Coons", "state":"Delaware", "birthdate":"09/09/63" }, | |
| { "name":"Dan Sullivan", "state":"Alaska", "birthdate":"11/13/64" }, | |
| { "name":"Michael Bennet", "state":"Colorado", "birthdate":"11/28/64" }, | |
| { "name":"Tim Scott", "state":"South Carolina", "birthdate":"09/19/65" }, | |
| { "name":"Kirsten Gillibrand", "state":"New York", "birthdate":"12/09/66" }, | |
| { "name":"James Lankford", "state":"Oklahoma", "birthdate":"03/04/68" }, | |
| { "name":"Kelly Ayotte", "state":"New Hampshire", "birthdate":"06/27/68" }, | |
| { "name":"Cory Booker", "state":"New Jersey", "birthdate":"04/27/69" }, | |
| { "name":"Joni Ernst", "state":"Iowa", "birthdate":"07/01/70" }, | |
| { "name":"Ted Cruz", "state":"Texas", "birthdate":"12/22/70" }, | |
| { "name":"Marco Rubio", "state":"Florida", "birthdate":"05/28/71" }, | |
| { "name":"Mike Lee", "state":"Utah", "birthdate":"06/04/71" }, | |
| { "name":"Martin Heinrich", "state":"New Mexico", "birthdate":"10/17/71" }, | |
| { "name":"Ben Sasse", "state":"Nebraska", "birthdate":"02/22/72" }, | |
| { "name":"Brian Schatz", "state":"Hawaii", "birthdate":"10/20/72" }, | |
| { "name":"Chris Murphy", "state":"Connecticut", "birthdate":"08/03/73" }, | |
| { "name":"Cory Gardner", "state":"Colorado", "birthdate":"08/22/74" }, | |
| { "name":"Tom Cotton", "state":"Arkansas", "birthdate":"05/13/77" } | |
| ]; | |
| var seconds_to_years = function(s) { | |
| return Math.floor(s / (60*60*24*365*1000)); | |
| }; | |
| var a_with_ages = _(a).map( function(o) { | |
| o.age = seconds_to_years(Date.now() - Date.parse(o.birthdate)); | |
| return o; | |
| }); | |
| var california = _(a_with_ages).filter({'state':'California'}) | |
| $('#cal').text( JSON.stringify( | |
| california | |
| )); | |
| var sum = function(total, x) { | |
| return total + x; | |
| }; | |
| $('#average').text( | |
| _(a_with_ages).pluck('age').reduce(sum) / a.length | |
| ); | |
| </script> | |
| </html> |
Author
johnkawakami
commented
Jun 4, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment