How to get the index of the highest value of an array?

Magusalfador

Veteran
Veteran
Joined
Jun 15, 2014
Messages
209
Reaction score
36
First Language
English Spanish
Primarily Uses
Hi all!

Im super new at javascript. I created an array and I want to know which index hold the highest number.
I got this code:
Code:
var a = [0, 21, 22, 7];
var indexOfMaxValue = a.reduce((iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0);

console.log("indexOfMaxValue = " + indexOfMaxValue);
When I try it on a javascript sandbox, it does what I want but If I try it on the RPGMAKER MV console, it sayts unexpected token "=>".
Im guessing I cant just run that code on the console? or maybe because im in version 1.51 that I cant run that specific code?

Any how, any help to achieve what I need would be appretiated.

Thanks!
 

dsiver144

Peaceful Days Dev
Veteran
Joined
Sep 19, 2013
Messages
261
Reaction score
809
First Language
Vietnamese
Primarily Uses
RMVXA
You can try this :D
Code:
var indexOfMaxValue = a.indexOf(Math.max.apply(null, a));
 

Magusalfador

Veteran
Veteran
Joined
Jun 15, 2014
Messages
209
Reaction score
36
First Language
English Spanish
Primarily Uses
Ha, so simple. Thanks!!!
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,072
First Language
English
Primarily Uses
RMMV
If I try it on the RPGMAKER MV console, it sayts unexpected token "=>".
That token is an arrow function, which was added in the 2015 specification of Javascript. MV 1.5 and earlier doesn't support this syntax, to use 2015+ features, you'd have to upgrade MV to 1.6

Another option is the spread operator (2015+):

Code:
a.indexOf(Math.max(...a))
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Is it okay to hug a tree? :/
The new intro Logo for my videos.

I speed it up, the one I use to use was to long.
The workweek is now over. Now I can work on my GamZzzzzZzzzzZzzzZzzZz....
Just watched Onward with the family. One of the best movies I've ever seen. <3

Forum statistics

Threads
95,824
Messages
932,212
Members
126,259
Latest member
Illuminachos
Top