JSFiddle - React, Tailwind, and code Playground
by fairmutex
HTML
<script src="http://eloquentjavascript.net/code/ancestry.js"></script>
JavaScript
//http: //eloquentjavascript.net/code/ancestry.js
var ancestry = JSON.parse(ANCESTRY_FILE);
console.log(ancestry);
function average(array) {
function plus(a, b) {
return a + b;
}
return array.reduce(plus) / array.length;
}
var byCentury = [];
ancestry.forEach(function (person) {
var century = Math.ceil(person.died / 100);
if (byCentury[century] === undefined)
byCentury[century] = [];
byCentury[century].push(Number(person.died) - Number(person.born));
});
console.log(byCentury);
var results = byCentury.map(average);
console.log(results);
function byBirth(p){
return p.born;
}
function byFather(p){
return p.father;
}
function groupBy(a,func){
var temp = [];
a.forEach(function(c){
var i = func(c)
if (temp[i] === undefined)
temp[i] = [];
temp[i].push(c);
}
);
return temp;
}
var result = groupBy(ancestry,byFather);
console.log(result);