Maximum element and subscript of an array

Source: Internet
Author: User

There are two things, especially useful, a call called "apply", this is two methods, very powerful.

The maximum number, of course, the minimum number is the same.

var arr=[1,2,3,4,5,6,10,7,9,11,20,19];

Alert (Math.min.apply (Null,arr));//Gets the minimum value in the array

Alert (Math.max.apply (Null,arr));//Gets the maximum value in the array

Ask for the maximum subscript

var index_ =arr.indexof (Math.max.apply (Math, arr)); To get the maximum value of the subscript similarly, the minimum subscript is the min.

So what's the principle?

Apply method: Syntax: Apply ([Thisobj[,argarray]]) Definition: Apply one method of an object, replace the current object with another object. Description: If Argarray is not a valid array or is not a arguments object, it will result in a TypeError. If none of the Argarray and Thisobj parameters are provided, then the Global object is used as a thisobj and cannot be passed any parameters.

The parameters of the apply reference must be in the form of an array, and the function and call are the same. Instead of replacing the current object with other objects, I can only understand that the current object has no function for the other object.

Reference: Portal

Maximum element and subscript of an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.