返回多个最高分的索引

时间:2015-10-13 20:40:50

标签: javascript arrays

我正在研究如何从数组中返回最高分数的索引,但我很难弄清楚我是否可以从此方法返回多个项目,或者我是否需要使用返回数组中两个最高分的索引的替代方法。

 var scores = [60, 50, 60, 58, 54, 54,
                  58, 50, 52, 54, 48, 69,
                  34, 55, 51, 52, 44, 51,
                  69, 64, 66, 55, 52, 61,
                  46, 31, 57, 52, 44, 18,
                  41, 53, 55, 61, 51, 44];

var tests = scores.length;
var highest;
for (var i = 0; i < scores.length; i++) {
  if (i === 0) {
    highest = scores[i];
  }
  if (scores[i] > highest) {
    highest = scores[i];
  }
}

5 个答案:

答案 0 :(得分:2)

返回包含索引的数组。当你得到一个高于最高值的元素时,清空数组并将其放入其中。当你得到一个等于最高的元素时,将i推入数组。

var highest = scores[0];
var indexes = [0];
for (var i = 1; i < scores.length; i++) {
    if (scores[i] > highest) {
        // Found new highest, reset everything
        indexes = [i];
        highest = scores[i];
    } else if (scores[i] == highest) {
        // Found duplicate highest, add its index to indexes array
        indexes.push(i);
    }
}

答案 1 :(得分:1)

// find the maximum value in an array:
var max = Math.max.apply( null, scores );  // 69
var maxindexes = [];
// iterate over the array and push the indexes of items matching the max to maxindexes
scores.forEach( function( score, idx ){
  if( score == max ){
    maxindexes.push( idx );   
  }
});  // [11,18]

source

答案 2 :(得分:0)

基本的Javscript方式是这样的:

var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44];

alert(getHighestIndexes(scores));    

function getHighestIndexes(scores)
{
    var result = [];
    for (var i = 0; i < scores.length; i++) 
    {
        if (i == 0)             
            result.push(scores[i]);                         
        else if (scores[i] > result[0])                     
            result = [i];                   
        else if (scores[i] == result[0])        
            result.push(i);         
    }
    return result;
}

但最佳做法是使用像lodash或underscore js这样的函数库,然后就可以这样做:

<script src="https://raw.githubusercontent.com/lodash/lodash/3.10.1/lodash.min.js" ></script>    
<script>
    var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18, 41, 53, 55, 61, 51, 44];  
    alert(getHighestIndexes(scores));

    function getHighestIndexes(scores)
    {
        var arrayAsObject = _.zip(scores, _.range(scores.length));
        return _.pluck(_.where(arrayAsObject , {"0" : _.max(scores)}), "1");    
    }
</script>   

答案 3 :(得分:0)

使用javascript .sort()函数从最高到最低排序,然后获取数组的前2个索引(或者你想要多少个)

var scores = [60, 50, 60, 58, 54, 54,
              58, 50, 52, 54, 48, 69,
              34, 55, 51, 52, 44, 51,
              69, 64, 66, 55, 52, 61,
              46, 31, 57, 52, 44, 18,
              41, 53, 55, 61, 51, 44];


var sorted = scores.sort(function(a, b){return b-a});
console.log(sorted);
alert('Two Highest Scores: ' + sorted[0] + ' and ' +  sorted[1]);

答案 4 :(得分:0)

另一种方法:找到max元素,然后将源数组减少到max元素的索引,就像这样

var arr = [60, 50, 60, 58, 54, 54,
          58, 50, 52, 54, 48, 69,
          34, 55, 51, 52, 44, 51,
          69, 64, 66, 55, 52, 61,
          46, 31, 57, 52, 44, 18,
          41, 53, 55, 61, 51, 44];
var max = Math.max.apply(Math,arr);
var res = arr.reduce(function(acc,cur,index){
    if(cur==max) acc.push(index);
    return acc;
},[]);