Java教程

求数组平均值 === 箭头函数用法

本文主要是介绍求数组平均值 === 箭头函数用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// js 原生方式声明函数

function say(name){
    console.log("hello" + name);
}



// 箭头函数

  var say = name => console.log("hello " + name);   // hello tom
  say("tom"); 

 
// 多个参数

function sum1(a, b) {
    return a + b;
}
var sum2 = (a, b) => a + b;
console.log(sum1(2, 3));  // 5
console.log(sum2(2, 3));   // 5
// 复杂函数声明

var MAX = (a, b) => {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}
// 箭头函数+解构表达式

person = {
    name: 'jerry',
    age: 12,
    language: ['java', 'python', 'go']
}
var hello = (person) => {
    console.log("hello "+person.name); // hello jerry
}
// 用结构表达式
var hi = ({name}) => {
    console.log("hi " + name);    // hi jerry
}
hello(person)
hi(person)

 

 

 

 

求数组平均值

 

const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
// arrayAverage([1,2,3]) -> 2

 

这篇关于求数组平均值 === 箭头函数用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!