Last active
October 23, 2015 14:07
-
-
Save balanza/3b3b3b7f40fef50dc8b1 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
//this is row data coming from db | |
var dataset = [{ | |
firstname: 'John', | |
lastname: 'Doe', | |
birth: '1986-02-09', | |
languages: ['en'], | |
sex: 'm' | |
}, { | |
firstname: 'Maria', | |
lastname: 'Rossi', | |
birth: '1982-03-05', | |
languages: ['it', 'es'], | |
sex: 'f' | |
}, { | |
firstname: 'Enrique', | |
lastname: 'Morales', | |
birth: '1990-06-10', | |
languages: ['en', 'es'], | |
sex: 'm' | |
}]; | |
//new_dataset is a new set of data which: | |
// - contains only spanish-speakers | |
// - is formatted with name, age and sex (as long text) | |
// - is ordered by age ascending | |
var new_dataset = _(dataset).chain() | |
//after this, dataset will contain only spanish-speaker | |
.filter(function(e) { | |
return e.languages.indexOf('es') >= 0; | |
}) | |
// after this, fileds will be formatted | |
.map(function(e) { | |
return { | |
name: [e.firstname, e.lastname].join(' '), | |
age: (function(birthday) { | |
var ageDifMs = Date.now() - birthday.getTime(); | |
var ageDate = new Date(ageDifMs); | |
return Math.abs(ageDate.getUTCFullYear() - 1970); | |
})(new Date(e.birth)), | |
sex: e.sex == 'm' ? 'Male' : 'Female' | |
}; | |
}) | |
//after this, elements will be ordered | |
.sortBy('age') | |
//this runs the chain | |
.value(); | |
console.log(new_dataset); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment