lis*_*aro 55 javascript arrays min
Array justPrices具有如下值:
[0] = 1.5
[1] = 4.5
[2] = 9.9.
Run Code Online (Sandbox Code Playgroud)
如何返回数组中的最小值?
Dar*_*rov 88
Jon Resig在本文中说明了如何通过扩展Array原型并调用底层的Math.min方法来实现这一点,不幸的是,该方法不采用数组而是采用可变数量的参数:
Array.min = function( array ){
return Math.min.apply( Math, array );
};
Run Code Online (Sandbox Code Playgroud)
然后:
var minimum = Array.min(array);
Run Code Online (Sandbox Code Playgroud)
zzz*_*Bov 74
查找最小值的最简洁的表达代码可能是休息参数:
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(...arr)
console.log(min)
Run Code Online (Sandbox Code Playgroud)
Function.prototype.apply
当您不需要更改函数的上下文时,Rest参数本质上是一种方便的简写:
var arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
var min = Math.min.apply(Math, arr)
console.log(min)
Run Code Online (Sandbox Code Playgroud)
这也是一个很好的用例Array.prototype.reduce
:
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = arr.reduce((a, b) => Math.min(a, b))
console.log(min)
Run Code Online (Sandbox Code Playgroud)
Math.min
直接传递给它可能很诱人reduce
,但回调会收到其他参数:
callback (accumulator, currentValue, currentIndex, array)
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,它可能有点冗长.reduce
当您有一组要复合到单个值的复杂数据时,此功能特别有用:
const arr = [{name: 'Location 1', distance: 14}, {name: 'Location 2', distance: 58}, {name: 'Location 3', distance: 20}, {name: 'Location 4', distance: 77}, {name: 'Location 5', distance: 66}, {name: 'Location 6', distance: 82}, {name: 'Location 7', distance: 42}, {name: 'Location 8', distance: 67}, {name: 'Location 9', distance: 42}, {name: 'Location 10', distance: 4}]
const closest = arr.reduce(
(acc, loc) =>
acc.distance < loc.distance
? acc
: loc
)
console.log(closest)
Run Code Online (Sandbox Code Playgroud)
当然,您始终可以使用经典迭代:
var arr,
i,
l,
min
arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
min = Number.POSITIVE_INFINITY
for (i = 0, l = arr.length; i < l; i++) {
min = Math.min(min, arr[i])
}
console.log(min)
Run Code Online (Sandbox Code Playgroud)
...但即使是经典的迭代也可以获得现代化的改造:
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
let min = Number.POSITIVE_INFINITY
for (const value of arr) {
min = Math.min(min, value)
}
console.log(min)
Run Code Online (Sandbox Code Playgroud)
小智 40
我发现返回数组最小值的最简单方法是在Math.min()函数上使用Spread Operator.
return Math.min(...justPrices);
//returns 1.5 on example given
Run Code Online (Sandbox Code Playgroud)
MDN上的页面有助于更好地理解它:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
一点额外:这也适用于Math.max()函数
返回Math.max(... justPrices); //给出的示例返回9.9.
希望这可以帮助!
c69*_*c69 13
更新:使用Darin的/ John Resig答案,请记住,您不需要指定thisArg min
,因此Math.min.apply(null, arr)
可以正常工作.
或者您可以对数组进行排序并获得值#1:
[2,6,7,4,1].sort()[0]
[!]但是,如果没有提供自定义数字排序功能,这只能在一个非常有限的情况下工作:正数小于10.看看它会如何破裂:
var a = ['', -0.1, -2, -Infinity, Infinity, 0, 0.01, 2, 2.0, 2.01, 11, 1, 1e-10, NaN];
// correct:
a.sort( function (a,b) { return a === b ? 0 : a < b ? -1: 1} );
//Array [NaN, -Infinity, -2, -0.1, 0, "", 1e-10, 0.01, 1, 2, 2, 2.01, 11, Infinity]
// incorrect:
a.sort();
//Array ["", -0.1, -2, -Infinity, 0, 0.01, 1, 11, 1e-10, 2, 2, 2.01, Infinity, NaN]
Run Code Online (Sandbox Code Playgroud)
而且,数组也就地更改,这可能不是您想要的.
想象一下你有这个数组:
var arr = [1, 2, 3];
Run Code Online (Sandbox Code Playgroud)
ES6方式:
var min = Math.min(...arr); //min=1
Run Code Online (Sandbox Code Playgroud)
ES5方式:
var min = Math.min.apply(null, arr); //min=1
Run Code Online (Sandbox Code Playgroud)
如果你使用D3.js,有一个方便的功能,它会做同样的事情,但会忽略未定义的值,并检查自然顺序:
d3.max(array [,accessor])
使用自然顺序返回给定数组中的最大值.如果数组为空,则返回undefined.可以指定可选的访问器函数,这相当于在计算最大值之前调用array.map(访问器).
与内置的Math.max不同,此方法忽略未定义的值; 这对忽略丢失的数据很有用.此外,使用自然顺序而不是数字顺序比较元素.例如,字符串["20","3"]的最大值为"3",而数字[20,3]的最大值为20.
这是D3 v4的源代码:
export default function(values, valueof) {
var n = values.length,
i = -1,
value,
max;
if (valueof == null) {
while (++i < n) { // Find the first comparable value.
if ((value = values[i]) != null && value >= value) {
max = value;
while (++i < n) { // Compare the remaining values.
if ((value = values[i]) != null && value > max) {
max = value;
}
}
}
}
}
else {
while (++i < n) { // Find the first comparable value.
if ((value = valueof(values[i], i, values)) != null && value >= value) {
max = value;
while (++i < n) { // Compare the remaining values.
if ((value = valueof(values[i], i, values)) != null && value > max) {
max = value;
}
}
}
}
}
return max;
}
Run Code Online (Sandbox Code Playgroud)
ES6是未来的发展方向.
arr.reduce((a, b) => Math.min(a, b));
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种形式,因为它很容易推广到其他用例
小智 5
var array =[2,3,1,9,8];
var minvalue = array[0];
for (var i = 0; i < array.length; i++) {
if(array[i]<minvalue)
{
minvalue = array[i];
}
}
console.log(minvalue);
Run Code Online (Sandbox Code Playgroud)