JavaScript 累加 reduce 计算全部年龄

const inventors = [{first: "Albert", last: "Einstein", year: 1879, passed: 1955}, {
    first: "Isaac", last: "NewTon", year: 1643, passed: 1727
}, {first: "Galileo", last: "Galilei", year: 1564, passed: 1642}, {
    first: "Marie", last: "Curie", year: 1847, passed: 1934
}, {first: "Johannes", last: "Lepler", year: 1571, passed: 1630}, {
    first: "Nicolaus", last: "Copermocis", year: 1473, passed: 1543
}, {first: "Max", last: "Planck", year: 1858, passed: 1947},]
console.log("计算全部年龄")
const totalYears = inventors.reduce((total, inventor) => {
    return total + (inventor.passed - inventor.year);
}, 0)
console.log(totalYears);

 

你可能感兴趣的:(#,javascript,javascript,开发语言,ecmascript)