JavaScript 數(shù)組中最大最小值
現(xiàn)在獲取數(shù)組中最大最小值用的越來越多了,于是乎我編了個方法供大家使用。代碼如下,若有問題可以與我聯(lián)系,咱們一起學習一起進步。
我們來看下示例一:
var numReg = /^-?[0-9]+.?[0-9]*$/
Array.prototype.min = function() {
return this.reduce(function(preValue, curValue,index,array) {
if ( numReg.test(preValue) && numReg.test(curValue) ) {
return preValue > curValue ? curValue : preValue;
} else if ( numReg.test(preValue) ) {
return preValue;
} else if ( numReg.test(curValue) ) {
return curValue;
} else {
return 0;
}
})
}
Array.prototype.max = function() {
return this.reduce(function(preValue, curValue,index,array) {
if ( numReg.test(preValue) && numReg.test(curValue) ) {
return preValue < curValue ? curValue : preValue;
} else if ( numReg.test(preValue) ) {
return preValue;
} else if ( numReg.test(curValue) ) {
return curValue;
} else {
return 0;
}
})
}
示例二:
function getMaximin (arr,maximin) {
if (maximin == "max") {
return Math.max.apply(Math, arr);
}else if (maximin == "min") {
return Math.min.apply(Math, arr);
}
}
var a = [3,2,4,2,10]
var b = [12,4,45,786,9,78]
alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4
function getMaximin (arr,maximin) {
if (maximin == "max") {
return Math.max.apply(Math, arr);
}else if (maximin == "min") {
return Math.min.apply(Math, arr);
}
}
var a = [3,2,4,2,10]
var b = [12,4,45,786,9,78]
alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4
我們再來看2個方法
方法一:
//最小值
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++){
if (this[i] < min){
min = this[i];
}
}
return min;
}
//最大值
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++){
if (this[i] > max) {
max = this[i];
}
}
return max;
}
如果你是引入類庫進行開發(fā),害怕類庫也實現(xiàn)了同名的原型方法,可以在生成函數(shù)之前進行重名判斷:
if (typeof Array.prototype['max'] == 'undefined') {
Array.prototype.max = function() {
... ...
}
}
方法二:
用Math.max和Math.min方法可以迅速得到結(jié)果。apply能讓一個方法指定調(diào)用對象與傳入?yún)?shù),并且傳入?yún)?shù)是以數(shù)組形式組織的。恰恰現(xiàn)在有一個方法叫Math.max,調(diào)用對象為Math,與多個參數(shù)
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
但是,John Resig是把它們做成Math對象的靜態(tài)方法,不能使用大神最愛用的鏈式調(diào)用了。但這方法還能更精簡一些,不要忘記,Math對象也是一個對象,我們用對象的字面量來寫,又可以省幾個比特了。
Array.prototype.max = function(){
return Math.max.apply({},this)
}
Array.prototype.min = function(){
return Math.min.apply({},this)
}
[1,2,3].max()// => 3
[1,2,3].min()// => 1
相關(guān)文章
Javascript中的window.event.keyCode使用介紹
我們之前發(fā)過不少關(guān)于event.keyCode相關(guān)的文章,大家都可以參考下。2011-04-04
JavaScript中變量提升導致未定義(undefined)的問題及解決方法
在 JavaScript 中,變量提升(Hoisting)是一個相對常見的行為,尤其是當你遇到 undefined 錯誤時,本文將詳細探討變量提升的概念、其對代碼執(zhí)行的影響以及如何避免因為變量提升而導致 undefined 的問題,需要的朋友可以參考下2024-09-09
微信小程序返回上一頁刷新組件數(shù)據(jù)的示例代碼
這篇文章主要介紹了微信小程序返回上一頁刷新組件數(shù)據(jù)的相關(guān)資料,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2024-03-03

