Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the average of es6 Array

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report