Home > Web Front-end > JS Tutorial > 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

青灯夜游
Release: 2021-10-13 16:49:55
Original
6413 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template