Prefaceis not provided in the array
arr.max()
And
arr.min()
Such a method. So is it possible to implement such a method in other ways? So today we're going to do some of the ways to take out the maximum and minimum values in the array.
Law One: in fact, the use of ECMAScript5 ... The expand operator is a simple solution to this problem
var arr=[2,7,3,ten,One,one// +//2
Law II:An array is traversed for the traversal of arrays, there are many different methods for comparing the various methods: array.prototype.max=function () {
array.prototype.max=function () {
Letmax= This[0];
This. ForEach (function (item,index) {
If(item>max) {
Max=item;
}
});
return Max;
}
vararr = [1, $, at,3,6,2,7,234, About,222,34444,9];console.time ("Time Consuming"); Console.log (Arr.max ()); //34444Console.timeend ("Time Consuming");//Time- consuming: 0.376ms
// ================================================ //
array.prototype.max=function () { Letmax= This[0]; This. Map (function (item,index) {if(item>max) {Max=item;}});returnMax;}vararr = [1, $, at,3,6,2,7,234, About,222,34444,9];console.time ("Time Consuming"); Console.log (Arr.max ()); //34444Console.timeend ("Time Consuming");//Time- consuming: 0.402ms
// ================================================= //
array.prototype.max=function () { Letmax= This[0]; for(varI=1;i< This. length;i++){if( This[i]>max) {Max= This[i];}}returnMax;}vararr = [1, $, at,3,6,2,7,234, About,222,34444,9];console.time ("Time Consuming"); Console.log (Arr.max ()); //34444Console.timeend ("Time Consuming");//Time- consuming: 0.522ms
Let's see what happens if the array is not full of values?
var arr = [1,45,23,3,6,2,7,234,56, ' 2345 ', 5, ' a ',+// "a"// "C" // +
Well, the results seem to be not what we want, after testing, I think the number and character size comparison is the rule: ① if the two numbers are numeric, according to the ordinary numerical comparison method ② If two numbers are strings, then two strings from the high-bit start comparison, according to ASCLL code size comparison; English letters > numbers, lowercase > uppercase letters"Note" The word representable ASCII code: with charCodeAt ();
// $ // the
③ if one is a string and one is a numeric value, the value is converted to a string and compared by the size comparison method of two strings
Law III:You can also use the Array.prototype.reduce method to traverse, using this method without adding additional Max variables
array.prototype.max=function() { return arr.reduce (function(prev, Next) { return prev>next? Prev:next; });} var arr = [1,45,23,3,6,2,7,234,56,222,34444,9];console.time ("time-consuming"// 34444 // Time- consuming: 0.389ms
To find the minimum value of the array is no longer said, the same easy to get ~
Summarize
The top of the array to find the most simple or law one, the rest is to iterate over the array, for the array of traversal also choose a good method, different traversal method performance, which Array.prototype.map is elegant, but the actual performance will not be better than the foreach, specific or case-determined.
Use JavaScript to find the maximum and minimum values of an array