Get the App
SLTechnology News&Howtos  ›  Development  › 

How to calculate the average value of javascript two-dimensional array

Shulou Source: shulou.com Published: 2022-06-03 03:45:08 10月03日 Update

This article mainly introduces "how to calculate the average value of javascript two-dimensional array". In the daily operation, I believe many people have doubts about how to find the average value of javascript two-dimensional array. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to find the average value of javascript two-dimensional array". Next, please follow the editor to study!

JS two-dimensional array to calculate the average method: 1, nested double-layer for loop through all the elements of the two-dimensional array; 2, in the loop body, use the "sum+=arr1 [I] [j]" statement to add and sum all the array elements; 3, divide the sum of array elements by the number of array elements to get the average.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Average value of javascript two-dimensional array

To require the average of a two-dimensional array, we need to get each element in the two-dimensional array, add them up, and divide by the number of elements.

So how do you get every element of a two-dimensional array?

When you see this, you think of the for loop, which uses array subscripts to get elements. Because it is a two-dimensional array, two layers of for loops are used for nesting: the first layer controls the array row subscript and the second layer controls the array column subscript.

Ok, let's look at the implementation code:

Var sum=0;var Ball0; / / this variable is defined to record the total number of elements in the array var arr= [[12 beats 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]; for (var iPersoni.

Tags: Array two-dimensional average element number learning loop subscript method more help control utility next sum two layers code two layers variables loop body Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS NVidia Redmi Xiaomi