function spliceMaxMin (arry){
var result = arry.splice(0),
max = Math.max(...result),
min = Math.min(...result)
for(var i = 0; i < result.length;i++){
if(result[i] == max){
result.splice(i,1)
}
if(result[i] ==min){
result.splice(i,1)
}
}
return result
}
内置函数Math.max()和Math.min()可以分别找出参数中的最大值和最小值。
这些函数对于数字组成的数组是不能用的。但是,这有一些类似地方法。
Function.prototype.apply()让你可以使用提供的this与参数组成的_数组(array)_来调用函数。
var numbers = [1, 2, 3, 4];
Math.max.apply(null, numbers) // 4
Math.min.apply(null, numbers) // 1
给apply()第二个参数传递numbers数组,等于使用数组中的所有值作为函数的参数。
一个更简单的,基于ES2015的方法来实现此功能,是使用展开运算符.
var numbers = [1, 2, 3, 4];
Math.max(...numbers) // 4
Math.min(...numbers) // 1
此运算符使数组中的值在函数调用的位置展开。
Math.max(1, 2, 3, 4); // 4
Math.min(1, 2, 3, 4); // 1
本文介绍了一种JavaScript方法,该方法从数组中移除最大和最小元素,并探讨了使用Math.max和Math.min函数的不同方式。
2842

被折叠的 条评论
为什么被折叠?



