JavaScript 累加 reduce 计算全部年龄

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 累加 reduce 计算全部年龄的主要内容,如果未能解决你的问题,请参考以下文章

javaScript中 数组的新方法(reduce)

javascript [数组数组的平均值]使用reduce()将每个值添加到累加器,使用值0初始化,除以a的长度

javascript reduce方法用的多么

JavaScript - reduce方法 (Array)

JavaScript map reduce

JavaScript——Array.prototype.reduce()