简体   繁体   English

获取javascript中值最高的数组key

[英]Get the array key with the highest value in javascript

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?如何在不重复数组的情况下获取具有最高值的元素的索引?

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 所做的那样。

Somebody has to look at each item in an unsorted array...有人必须查看未排序数组中的每个项目...

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);

Get the array key with the highest value in javascript获取javascript中值最高的数组key

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

If the array is not ordered you cannot do this without iterating.如果数组未排序,则不进行迭代就无法执行此操作。

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;
    }
}

You could use a function to set the variable.您可以使用 function 来设置变量。 And keep track of the max in that function.并跟踪 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.缺点是它不自然,需要您使用 function 才能获得正确的结果。

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.如果看起来代码太多,请将其放在单独的例程中。

Is old question but here is an my simple emulation of PHP script max() made in javascript:是老问题,但这是我在 javascript 中制作的 PHP 脚本max()的简单模拟:

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 0 if 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

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.您使用两个变量进行冷优化,一个用于 position,另一个用于内容。

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.并使用该arr.mpush(v)来填充您的数组。 In this case the array will have maxIndex property.在这种情况下,数组将具有 maxIndex 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM