Reduce to find total age
Reduce to find total age of the person. The name may have repeated in the initial array.
by lshettyl
JavaScript
var users = [
{id: 1, name:'Julien', birthYear: 2010 },
{id: 2, name:'Lokesh', birthYear: 2014 },
{id: 3, name:'Andrea', birthYear: 2009 },
{id: 4, name:'Karina', birthYear: 2001 },
{id: 5, name:'Rajesh', birthYear: 2008 },
{id: 6, name:'Julien', birthYear: 2016 },
{id: 7, name:'Andrea', birthYear: 2001 }
];
let result = (name="Lokesh") => users.reduce((res, cv, ci, arr) => {
return res + (cv['name'] === name && new Date().getFullYear() - cv.birthYear || 0);
}, 0);
console.log(result("Julien"));