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  >  Article  >  Web Front-end  >  How to find the maximum value of an array and its subscript in javascript

How to find the maximum value of an array and its subscript in javascript

青灯夜游
青灯夜游Original
2021-10-13 16:49:556266browse

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.

How to find the maximum value of an array and its subscript in javascript

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:

How to find the maximum value of an array and its subscript in javascript

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn