獲取數(shù)組最大值
var ary = [12, 23, 34, 24, 35, 14, 25, 36];
// 方法一:首先給數(shù)組從小到大排序,然后第一個是最小值郑兴,最后一個是最大值
ary.sort(function (a, b) {
return a - b;
})
var min = ary[0];
var max = ary[ary.length - 1];
console.log(min, max);
// 方法二:假設(shè)法: 假設(shè)當前數(shù)組中的第一個值是最大值犀斋,然后拿這個值后面項逐一比較,如果后面某一個值比假設(shè)的還要大情连,說明假設(shè)錯了叽粹,這樣就把假設(shè)的值進行替換,使其成為新的最大值。
// -> 這種方法和自定義屬性一樣虫几,都是JavaScript中最常用的編程思想
var max = ary[0];
var min = ary[0];
for (var i = 1; i < ary.length; i++) {
var cur = ary[i];
if (cur > max) {
max = cur;
}
if (cur < min) {
min = cur;
}
}
console.log(min, max);
// 方法三:使用Math.max和Math.min方法實現(xiàn)
// 它們是在執(zhí)行的時候锤灿,把需要比較的那堆數(shù)一個個的傳遞進來,這樣擦可以得到最后的結(jié)果持钉,
// 一下子放一個ary數(shù)組是不可以的
// console.log(Math.min(ary));
// console.log(Math.max(ary));
// -> 第一次嘗試
console.log(Math.min([12, 23, 34, 24, 35, 14, 25, 36]));
// -> join/toString: 將這個數(shù)組轉(zhuǎn)化為 -> "12, 23, 34, 24, 35, 14, 25, 36"
// -> eval: 把一個字符串轉(zhuǎn)變?yōu)镴avaScript表達式執(zhí)行
// 例如: eval("1+1") -> 2
// eval("12, 23, 34, 24, 35, 14, 25, 36"); -> 只取出了最后一項的值
console.log(Math.min(12, 23, 34, 24, 35, 14, 25, 36));
console.log(Math.max(12, 23, 34, 24, 35, 14, 25, 36));
// 第二次嘗試:
console.log(eval("Math.max(" + ary.toString() + ");"));
// 第三次嘗試
var max = Math.max.apply(null, ary);
var min = Math.min.apply(null, ary);
console.log(max, min);
取平均值
// 類數(shù)組轉(zhuǎn)化為數(shù)組以及類數(shù)組借用數(shù)組方法的各種方式:
// -> 模擬內(nèi)置的slice實現(xiàn)數(shù)組克隆的功能
Array.prototype.mySlice = function () {
// this -> 當前要操作的這個數(shù)組ary
var ary = [];
for (var i = 0; i < this.length; i++) {
ary[ary.length] = this[i];
}
return ary;
}
// arguments.sort(); -> 報錯衡招,TypeError, arguments是類數(shù)組集合,它不是數(shù)組每强,不能直接使用數(shù)組的方法
function avgFn() {
// 1. 將類數(shù)組轉(zhuǎn)換為數(shù)組: 把arguments克隆一份一模一樣的數(shù)組出來
var ary = [];
for (var i = 0; i < arguments.length; i++) {
ary[ary.length] = arguments[i];
}
// 1. 將類數(shù)組轉(zhuǎn)換為數(shù)組(優(yōu)化版)
// -> 借用數(shù)組原型上的slice方法始腾,當slice執(zhí)行的時候,讓方法中的this變?yōu)橄胍幚淼腶rguments空执,實現(xiàn)將類數(shù)組arguments轉(zhuǎn)化為數(shù)組
var ary = Array.prototype.slice.call(arguments);
// 或者
// var ary = [].prototype.slice.call(arguments);
// 2. 給數(shù)組排序浪箭,去掉開頭和結(jié)尾,剩下的是求平均數(shù)
ary.sort(function (a, b) {
return a - b;
});
ary.shift();
ary.pop();
// 2. 給數(shù)組排序辨绊,去掉開頭和結(jié)尾奶栖,剩下的是求平均數(shù)(優(yōu)化版)
Array.prototype.sort.call(arguments, function (a, b) {
return a - b;
});
[].prototype.shift.call(arguments);
[].prototype.pop.call(arguments);
return (eval(ary.join("+")) / ary.length).toFixed(2);
// return 優(yōu)化
return (eval([].join.call(arguments, "+")) / arguments.length).toFixed(2);
}
avgFn(9.8, 9.7, 10, 9.9, 9.0, 9.8, 3.0);
類數(shù)組轉(zhuǎn)換為數(shù)組
var oLis = document.getElementsByTagName('div');
console.dir(oLis); // -> HTMLCollection元素集合類的一個實例 -> 它也是一個類數(shù)組集合
oLis = document.getElementsByName('nameVale');
console.dir(oLis); // -> NodeList節(jié)點集合 -> 它也是一個類數(shù)組集合
// 把元素集合轉(zhuǎn)換為數(shù)組
// -> 標準瀏覽器
var ary = Array.prototype.slice.call(oLis);
console.log(ary);
// -> 在IE6-8中,不支持借助數(shù)組的slice實現(xiàn)將元素集合或者節(jié)點集合的類數(shù)組轉(zhuǎn)換為數(shù)組
// -> 但是對于arguments借用數(shù)組的方法是不存在任何兼容性問題
// 非標準瀏覽器下解決方案
var ary = [];
for (var i = 0; i < oLis.length; i++) {
ary[ary.length] = oLis[i];
}