item).indexOf(max)" statement according to the maximum value Find its subscript value."/> item).indexOf(max)" statement according to the maximum value Find its subscript value.">
Home >Web Front-end >JS Tutorial >How to find the maximum value of an array and its subscript in javascript
Method: 1. Use the "Math.max(...arr)" statement to find the maximum value max of the arr array; 2. Use "arr.map(item => item).indexOf(max )" statement finds its subscript value based on the maximum value.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript finds the maximum value of an array and its subscript
Implementation ideas:
1 , Use the Math.max() method to find the maximum value of the array
2. Get its subscript in the array based on the maximum value
Implementation code:
var arr=[2,6,1,5,22,3,66,12,9]; var max=Math.max(...arr); console.log("最大值为:"+max); var index = arr.map(item => item).indexOf(max); console.log("最大值的数组下标为:"+index);
Output result:
[Recommended learning: javascript advanced tutorial】
The above is the detailed content of How to find the maximum value of an array and its subscript in javascript. For more information, please follow other related articles on the PHP Chinese website!