javascript實(shí)現(xiàn)數(shù)組最大值和最小值的6種方法
給定一個(gè)數(shù)組[1,8,5,4,3,9,2],編寫一個(gè)算法,得到數(shù)組的最大值 9,和最小值 1。
1、通過prototype屬性擴(kuò)展min()函數(shù)和max()函數(shù)算法1的思路是在自定義min()和max()函數(shù)中,通過循環(huán)由第一個(gè)值依次與后面的值作比較,動(dòng)態(tài)更新最大值和最小值,從而找到結(jié)果。
// 最小值A(chǔ)rray.prototype.min = function () { let min = this[0]; let len = this.length; for (let i = 1; i < len; i++) {if (this[i] < min) min = this[i] } return min}// 最大值A(chǔ)rray.prototype.max = function () { let max = this[0]; let len = this.length; for (let i = 1; i < len; i++) {if (this[i] > max) max = this[i] } return max}// 結(jié)果console.log(arr.min()); // 1console.log(arr.max()); // 92、借助Math對象的min()函數(shù)和max()函數(shù)
算法2的主要思路是通過apply()函數(shù)改變函數(shù)的執(zhí)行體,將數(shù)組作為參數(shù)傳遞給apply()函數(shù)。這樣數(shù)組就可以直接調(diào)用Math對象的min()函數(shù)和max()函數(shù)來獲取返回值。
Array.min = function(array) { return Math.min.apply(Math, array)}// 最大值A(chǔ)rray.max = function (array) { return Math.max.apply(Math, array)}// 結(jié)果console.log(Array.min(arr)); // 1console.log(Array.max(arr)); // 93、算法2的優(yōu)化
在算法2中將min()函數(shù)和max()函數(shù)作為Array類型的靜態(tài)函數(shù),但不支持鏈?zhǔn)秸{(diào)用,我們可以利用對象字面量進(jìn)行簡化。
// 最小值A(chǔ)rray.prototype.min = function() { return Math.min.apply({}, this)}// 最大值A(chǔ)rray.prototype.max = function () { return Math.max.apply({}, this)}// 結(jié)果console.log(arr.min()); // 1console.log(arr.max()); // 9
與算法2不同的是,在驗(yàn)證時(shí),因?yàn)閙in()函數(shù)和max()函數(shù)屬于實(shí)例方法,所以可以直接通過數(shù)組調(diào)用。上面的算法代碼中apply()函數(shù)傳入的第一個(gè)值為{},實(shí)際表示當(dāng)前執(zhí)行環(huán)境的全局對象。第二個(gè)參數(shù)this指向需要處理的數(shù)組。由于apply函數(shù)的特殊性第一個(gè)參數(shù),指定為 null 或 undefined 時(shí)會自動(dòng)替換為指向全局對象,原始值會被包裝。所以我們也可以將第一個(gè)參數(shù)設(shè)置為null、undefind。
4、借助Array類型的reduce()函數(shù)算法4的主要思想是reduce()函數(shù)不設(shè)置initialValue初始值,將數(shù)組的第一個(gè)元素直接作為回調(diào)函數(shù)的第一個(gè)參數(shù),依次與后面的值進(jìn)行比較。當(dāng)需要找最大值時(shí),每輪累加器返回當(dāng)前比較中大的值;當(dāng)需要找最小值時(shí),每輪累加器返回當(dāng)前比較中小的值。
// 最小值A(chǔ)rray.prototype.min = function () { return this.reduce((pre, cur) => {return pre < cur ? pre : cur })}// 最大值A(chǔ)rray.prototype.max = function () { return this.reduce((pre, cur) => {return pre > cur ? pre : cur })}// 結(jié)果console.log(arr.min()); // 1console.log(arr.max()); // 95、借助Array類型的sort()函數(shù)
算法5的主要思想時(shí)借助數(shù)組的原生sort()函數(shù)對數(shù)組進(jìn)行排序,排序完成后首尾元素即是數(shù)組的最小、最大元素。默認(rèn)的sort()函數(shù)在排序時(shí)時(shí)按照字母順序排序的,數(shù)字會被按照字符串處理。例如數(shù)字 18 會被當(dāng)做'18'處理,數(shù)字 6 被當(dāng)'6'來處理,在排序時(shí)是按照字符串的每一位進(jìn)行比較的,因?yàn)?1'比'6'要小,所以'11'排序時(shí)要比'6'小。對于數(shù)值類型的數(shù)組來說,這顯然不合理。所以我們需要進(jìn)行自定義排序。
let sortArr = arr.sort((a, b) => a - b)// 最小值sortArr[0]// 最大值sortArr[sortArr.length - 1]// 結(jié)果console.log(sortArr[0]); // 1console.log(sortArr[sortArr.length - 1]); // 96、借助ES6的擴(kuò)展運(yùn)算符
// 最小值Math.min(...arr)// 最大值Math.max(...arr)// 結(jié)果console.log(Math.min(...arr)); // 1console.log(Math.max(...arr)); // 9
到此這篇關(guān)于javascript實(shí)現(xiàn)數(shù)組最大值和最小值的6種方法的文章就介紹到這了,更多相關(guān)javascript 數(shù)組最大值和最小值內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
