Javascript 获取javascript中具有最高值的数组键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5850956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:12:07  来源:igfitidea点击:

Get the array key with the highest value in javascript

javascriptjqueryarrays

提问by Alexa

I have a array like

我有一个像

arr[1] = 234;
arr[2] = 345;
...

arr[40] = 126;

How can I get the index of the element with the highest value without reiterating the array?

如何在不重复数组的情况下获取具有最高值的元素的索引?

回答by kennebec

You can apply Math.max and pass the array as its arguments-

您可以应用 Math.max 并将数组作为其参数传递-

arr.indexOf(Math.max.apply(window,arr))

But now Math.max is doing the iterating, just as sort would do.

但是现在 Math.max 正在执行迭代,就像 sort 一样。

Somebodyhas to look at each item in an unsorted array...

有人必须查看未排序数组中的每个项目...

回答by Dario Barrionuevo

With jQuery, is as simple as:

使用 jQuery,就像这样简单:

// Get the max value from the array    
maxValue = Math.max.apply(this, arr);

// Get the index of the max value, through the built in function inArray
$.inArray(maxValue,arr);

回答by Darin Dimitrov

If the array is not ordered you cannot do this without iterating.

如果数组未排序,则不进行迭代就无法执行此操作。

回答by kock's patel

Get the array key with the highest value in javascript

获取javascript中具有最高值的数组键

var cars = ["Saab", "Volvo", "BMW"];
var max_car_result = cars[cars.length-1];
alert(max_car_result);

回答by thomasa88

Try this:

尝试这个:

var max_index = -1;
var max_value = Number.MIN_VALUE;
for(var i = 0; i < arr.length; i++)
{
    if(arr[i] > max_value)
    {
        max_value = arr[i];
        max_index = i;
    }
}

回答by John Hoven

You could use a function to set the variable. And keep track of the max in that function. Here's a quick example without type checking, testing, or support for removing a value.

您可以使用函数来设置变量。并跟踪该函数中的最大值。这是一个没有类型检查、测试或删除值支持的快速示例。

Array.prototype.maxValue = null;

Array.prototype.setIndex = function(index, value){
  this[index] = value;
  if (value > this.maxValue || this.maxValue == null)
    this.maxValue = value;
}


var arr = new Array();
arr.setIndex(0, 234);
arr.setIndex(1, 500);
arr.setIndex(2, -5);

var maxValue = arr.maxValue;

Obviously this is nicer if you're currently setting items like this:

显然,如果您当前正在设置这样的项目,这会更好:

var arr = new Array();
arr[0] = 1;
arr[1] = 500;
arr[2] = 2;

Rather than this:

而不是这样:

var arr = { 1, 500, 2 };

The downside is its not natural and requires you to use function to get the correct results.

缺点是它不自然,需要您使用函数来获得正确的结果。

回答by Bill K

Keep the array sorted or use a heap.

保持数组排序或使用堆。

Otherwise iterate. Even if you found some trick to do it it would still require iterating underneath so why not iterate?

否则迭代。即使您找到了一些技巧来做到这一点,它仍然需要在下面进行迭代,所以为什么不进行迭代呢?

If it seems like too much code, put it in a separate routine.

如果看起来代码太多,请将其放在单独的例程中。

回答by Pih

Two solution: to sort descending order and get the first element or:

两种解决方案:按降序排序并获取第一个元素或:

function bigger(array) {
  if (array.length < 1) {
    return -1;
  }
  bigger = 0;
  for(var i=1; i<array.length;i++ ) { 
    if(array[i] > array[bigger]) {
      bigger = i;
    }
  }
  return bigger;
}

you cold optimize using two variables, one for the position and other for the content.

您使用两个变量进行冷优化,一个用于位置,另一个用于内容。

回答by Ivijan Stefan Stipi?

Is old question but here is an my simple emulation of PHP script max()made in javascript:

是个老问题,但这是我max()用 javascript 制作的 PHP 脚本的简单模拟:

function max(array){
    if(Object.prototype.toString.call( array ) === '[object Array]'){
        return array[(array.length-1)];
    }
    else return 0;
}

This return value of last key in array or 0if nothing found.

数组中最后一个键的返回值,或者0如果没有找到。

Maby someone helps.

梅比有人帮忙。

You can use it like:

你可以像这样使用它:

var array = ['bananaman','spiderman','ironman','superman','batman','manman'];
var getLast = max(array);
if(getLast !== 0)
    alert(getLast); // manman

回答by c-smile

Either you will have iteration somewhere (in your code or in JQuery.each()) or you can define something like this:

您将在某处(在您的代码或 JQuery.each() 中)进行迭代,或者您可以定义如下内容:

Array.prototype.mpush = function(v)
{
  var maxv = this.maxValue || Number.MIN_VALUE;
  if( v > maxv ) { this.maxValue = v; this.maxIndex = this.length; }
  this.push(v);
}

and use that arr.mpush(v)to populate your array. In this case the array will have maxIndex property.

并使用它arr.mpush(v)来填充您的数组。在这种情况下,数组将具有 maxIndex 属性。