如何找到数组最小值的id?

时间:2022-11-29 13:11:32

I know that you can have the minimum value of an array by typing

我知道你可以通过输入获得数组的最小值

var min = Math.min.apply(null, array)

but this will return the smallest value of an array and not the id of this one for exemple if I have these values:

但是如果我有这些值,这将返回一个数组的最小值而不是这个例子的id:

array[1] = 24;
array[2] = 45;

I want it to return 1 (the ID holding the minimum value) but idk how to do, could someone help me with this issue please?

我希望它返回1(持有最小值的ID)但idk怎么做,有人可以帮我解决这个问题吗?

5 个解决方案

#1


4  

var index = array.indexOf(Math.min.apply(null, array));

#2


3  

You can use Array#reduce() to get the smallest number, while avoiding holes in the Array if needed.

您可以使用Array#reduce()来获取最小的数字,同时在需要时避免数组中的漏洞。

array.reduce(function(obj, n, i) {
    if (n < obj.min)
        obj.i = i;
    return obj;
}, {min:Infinity,i:-1}).i;

Or if performance and compatibility is a concern, you could just loop.

或者如果性能和兼容性是一个问题,你可以循环。

var res = -1;
var min = Infinity;

for (var i = 0; i < array.length; i++) {
    if ((i in array) && array[i] < min) {
         min = array[i];
         res = i;
    }
}

#3


2  

You can do it like this:

你可以这样做:

var id = array.indexOf(Math.min.apply(null, array));

#4


0  

Once you've got the value, you can use indexOf to get the index, like this:

获得该值后,可以使用indexOf获取索引,如下所示:

var index = array.indexOf(Math.min.apply(null, array));

You should be aware that indexOf was only recently included in JavaScript (ES5/JS 1.6 to be precise) so you may want to find some wrapper for it if the function does not exist.

您应该知道indexOf最近才包含在JavaScript(确切地说是ES5 / JS 1.6)中,因此如果函数不存在,您可能希望找到它的一些包装器。

See the MDN for more information (which contains an example implementation of a backwards compatible function).

有关更多信息,请参阅MDN(其中包含向后兼容功能的示例实现)。

#5


0  

Just like the algorithm for finding the min value, but you have to track the minimum index as well

就像查找最小值的算法一样,但您也必须跟踪最小索引

function minIndex(arr) {
    if (!arr || arr.length === 0) {
        return -1;
    }
    var min = arr[0];
    var minIndex = 0;
    for (var len = arr.length; len > 0; len--) {
        if (arr[len] < min) {
            min = arr[len];
            minIndex = len;
        }
    }
    return minIndex;
}

check out this fiddle

看看这个小提琴

#1


4  

var index = array.indexOf(Math.min.apply(null, array));

#2


3  

You can use Array#reduce() to get the smallest number, while avoiding holes in the Array if needed.

您可以使用Array#reduce()来获取最小的数字,同时在需要时避免数组中的漏洞。

array.reduce(function(obj, n, i) {
    if (n < obj.min)
        obj.i = i;
    return obj;
}, {min:Infinity,i:-1}).i;

Or if performance and compatibility is a concern, you could just loop.

或者如果性能和兼容性是一个问题,你可以循环。

var res = -1;
var min = Infinity;

for (var i = 0; i < array.length; i++) {
    if ((i in array) && array[i] < min) {
         min = array[i];
         res = i;
    }
}

#3


2  

You can do it like this:

你可以这样做:

var id = array.indexOf(Math.min.apply(null, array));

#4


0  

Once you've got the value, you can use indexOf to get the index, like this:

获得该值后,可以使用indexOf获取索引,如下所示:

var index = array.indexOf(Math.min.apply(null, array));

You should be aware that indexOf was only recently included in JavaScript (ES5/JS 1.6 to be precise) so you may want to find some wrapper for it if the function does not exist.

您应该知道indexOf最近才包含在JavaScript(确切地说是ES5 / JS 1.6)中,因此如果函数不存在,您可能希望找到它的一些包装器。

See the MDN for more information (which contains an example implementation of a backwards compatible function).

有关更多信息,请参阅MDN(其中包含向后兼容功能的示例实现)。

#5


0  

Just like the algorithm for finding the min value, but you have to track the minimum index as well

就像查找最小值的算法一样,但您也必须跟踪最小索引

function minIndex(arr) {
    if (!arr || arr.length === 0) {
        return -1;
    }
    var min = arr[0];
    var minIndex = 0;
    for (var len = arr.length; len > 0; len--) {
        if (arr[len] < min) {
            min = arr[len];
            minIndex = len;
        }
    }
    return minIndex;
}

check out this fiddle

看看这个小提琴