Hello everyone,
In the PG below, I’m attempting to retrieve the highest metadata value from an array of meshes. I researched JavaScript tutorials, and this seems relatively easy for a standard array using Math.max(array). However, it doesn’t seem to work in this PG Clicking on a mesh returns the sphere’s metadata “value,” and that doesn’t always match what Math.max returns.
https://www.babylonjs-playground.com/#4UUDKU#5
Math.max
takes two or more parameters and returns the maximum of the values.
In your PG, you pass a single parameter, so it will always return this value.
I have updated your PG:
https://www.babylonjs-playground.com/#4UUDKU#6
2 Likes
JohnK
June 28, 2020, 11:13am
3
In this line
var mostValue = Math.max(sphereArray[i].metadata.value.toFixed(0));
Math.max is not being fed an array, it is only being fed a single value which is of course its own max value.
You could use
var mostValue = -Infinity;
for (var i = 0; i < sphereArray.length; i++) {
mostValue = Math.max(sphereArray[i].metadata.value.toFixed(0), mostValue);
};
text2.text = mostValue.toString();
or
var mostValue = Math.max(...sphereArray.map((entry) => entry.value.toFixed(0));
text2.text = mostValue.toString();
Have rearranged your GUI so it is viewable https://www.babylonjs-playground.com/#4UUDKU#7
See spread operator for the use of …
I see @Evgeni_Popov beat me to it while I was doing the PG
2 Likes
Thanks @JohnK and @Evgeni_Popov for the explanations and the PG corrections, I really appreciate it.
I see now that a range has to be set.
Thanks again!
2 Likes