Array.prototype.max = function() {return Math.max.apply(null, this);};
Array.prototype.min = function() {return Math.min.apply(null, this);};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +`\nMin value is: ${p.min()}`);
var min = null;var max = null;for (var i = 0, len = arr.length; i < len; ++i){var elem = arr[i];if (min === null || min > elem) min = elem;if (max === null || max < elem) max = elem;}alert( "min = " + min + ", max = " + max );
Array.prototype.range = function() {
var min = null,max = null,i, len;
for (i = 0, len = this.length; i < len; ++i){var elem = this[i];if (min === null || min > elem) min = elem;if (max === null || max < elem) max = elem;}
return { min: min, max: max }};
用作
var arr = [3, 9, 22, -7, 44, 18, 7, 9, 15];
var range = arr.range();
console.log(range.min);console.log(range.max);
// decorate Math.max(function (oldMax) {Math.max = function () {this.foo(); // call Math.foo, or at least that's what we want
return oldMax.apply(this, arguments);};})(Math.max);
Math.foo = function () {print("foo");};
Array.prototype.max = function() {return Math.max.apply(null, this); // <-- passing null as the context};
var max = [1, 2, 3].max();
print(max);
function arrayMin(arr) {return arr.reduce(function (p, v) {return ( p < v ? p : v );});}
function arrayMax(arr) {return arr.reduce(function (p, v) {return ( p > v ? p : v );});}
function GetMaxDISCRETE(A){ var MaxX=A[0];
for (var X=0;X<A.length;X++)if (MaxX<A[X])MaxX=A[X];
return MaxX;}
function GetMaxAPPLY(A){ return Math.max.apply(null,A);}
function GetMaxREDUCE(A){ return A.reduce(function(p,c){ return p>c?p:c;});}
// For regular arrays:var max = Math.max(...arrayOfNumbers);
// For arrays with tens of thousands of items:let max = testArray[0];for (let i = 1; i < testArrayLength; ++i) {if (testArray[i] > max) {max = testArray[i];}}
function arrayMax(array) {return array.reduce(function(a, b) {return Math.max(a, b);});}
function arrayMin(array) {return array.reduce(function(a, b) {return Math.min(a, b);});}
在ES6中:
function arrayMax(array) {return array.reduce((a, b) => Math.max(a, b));}
function arrayMin(array) {return array.reduce((a, b) => Math.min(a, b));}
function minArray(a, key) {var min, i, j, index=0;if(!key) {min=a[0];for(i=0,j=a.length;i<j;i++){min=a[i]<min?a[i]:min;}return min;}min=a[0][key];for(i=0,j=a.length;i<j;i++){if(a[i][key]<min) {min = a[i][key];index = i;}}return index;}
var a = [{fee: 9}, {fee: 2}, {fee: 5}];
minArray(a, "fee"); // returns 1, as 1 is the proper array index for the 2nd array element.
function maxValue(arr) {return arr.reduce((max, val) => max > val ? max : val)}
或者更好的运行时:
function maxValue(arr) {let max = arr[0];
for (let val of arr) {if (val > max) {max = val;}}return max;}
或者同时获取min和max:
function getMinMax(arr) {return arr.reduce(({min, max}, v) => ({min: min < v ? min : v,max: max > v ? max : v,}), { min: arr[0], max: arr[0] });}
或者使用更好的运行时*:
function getMinMax(arr) {let min = arr[0];let max = arr[0];let i = arr.length;
while (i--) {min = arr[i] < min ? arr[i] : min;max = arr[i] > max ? arr[i] : max;}return { min, max };}
let array = [0,20,45,85,41,5,7,85,90,111];
let maximum = array.sort((a, b) => b - a)[0];let minimum = array.sort((a, b) => a - b)[0];
console.log(minimum, maximum)
let arr = [1,10,25,15,31,5,7,101];let sortedArr = arr.sort((a, b) => a - b)
let min = sortedArr[0];let max = sortedArr[arr.length-1]
console.log(`min => ${min}. Max => ${max}`)
class SmallestIntegerFinder {findSmallestInt(args) {return args.reduce((min,item)=>{ return (min<item ? min : item)});}}
class SmallestIntegerFinder {findSmallestInt(args) {return Math.min(...args)}}
class SmallestIntegerFinder {findSmallestInt(args) {return Math.min.apply(null, args);}}
class SmallestIntegerFinder {findSmallestInt(args) {args.sort(function(a, b) {return a - b; } )return args[0];}}
function arrayMax<T>(values?: (T | null | undefined)[]): T | undefined {const nonEmptyValues = filterEmpty(values);if (nonEmptyValues.length === 0) {return undefined;}return nonEmptyValues.reduce((a, b) => (a >= b ? a : b), nonEmptyValues[0]);}
function arrayMin<T>(values?: (T | null | undefined)[]): T | undefined {const nonEmptyValues = filterEmpty(values);if (nonEmptyValues.length === 0) {return undefined;}return nonEmptyValues.reduce((a, b) => (a <= b ? a : b), nonEmptyValues[0]);}
function filterEmpty<T>(values?: (T | null | undefined)[] | null): T[] {return values?.filter(notEmpty) ?? [];}
function notEmpty<T>(value: T | null | undefined): value is T {if (value === null || value === undefined) return false;const testDummy: T = value;return true;}