Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the average of es6 Array

Shulou Source: shulou.com Published: 2022-05-31 18:11:33 10月03日 Update

This article mainly introduces the es6 array how to average the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will read this es6 array how to average the article will have some gains, let's take a look at it.

Methods: 1. Use the "arr.reduce(function(p,c){return p+c;})" statement to calculate the sum of all elements in the array;2. Use the "arr.length" statement to calculate the number of elements in the array;3. Use the "element and/element number" statement to calculate the average number of elements in the array.

Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

A method of calculating the average number of es6 arrays

Calculate the sum of all elements in an array using reduce().

var arr = [1,2,3,4,5,6,7,8,9,10];console.log(arr);var r = arr.reduce(function(pre, curr) { return pre + curr;});console.log("sum of array elements is: "+r);

2. Use the length attribute to get the number of elements in the array, that is, the array length.

var len=arr.length;console.log("Number of array elements is: "+len);

Divide the sum of elements by the number of elements to calculate the average value.

var average=r/len;console.log("array average is: "+average);

About "es6 array how to find the average" the content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to average es6 array" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.

Tags: Array element average number sum knowledge statement content average method article value function attribute easy to operate tutorial article easy to understand more environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Linux OPPO Reno Xiaomi