Godot - Getting the Average Value of an Array
This code example shows how to get the average value of an array in GDScript.
Use `reduce()
` to get the sum of an array.
`reduce()
` calls the specified Callable for each element in the array and accumulates the result.
Divide the obtained sum by the number of elements in the array obtained with `size()
`.
var array = [5, 6, 7]
var sum = array.reduce(func(acc, num): return acc + num)
var average = sum / array.size()
print(average)
# 6