返回数组中最大值的索引

时间:2016-03-02 09:57:30

标签: javascript arrays

我想要一个返回数组最大值的函数。 我知道这个问题被多次询问过,但是在一个数组中我们有多于一个最高值的情况呢? 让我们假设我们的数组看起来像这样:

var arr = [1,10,2,10];

功能:

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

只会返回索引1,但我想得到1和3。

最短且最有效的方法是什么。

7 个答案:

答案 0 :(得分:4)

const largest = Math.max(...arr);
arr.reduce((indexes, n, index) => {
  return indexes.concat(n === largest ? [index] : []);
}, []);

或者,如果ES5是你的一杯茶:

var largest = Math.max.apply(Math, arr);
arr.reduce(function(indexes, n, index) {
  return indexes.concat(n === largest ? [index] : []);
}, []);

或者,如果是通用函数:

function findAllIndexes(arr, val) {
  return arr.reduce((indexes, element, index) => {
    if(element === val) {
      return indexes.concat([element]);
    } else {
      return indexes;
    }
  }, []);
}

findAllIndexes(arr, Math.max(..arr));

答案 1 :(得分:2)

“手动”方式:

    var getIndicesOfHighest = function(arrIn) {
        var indices = [];
        var highest = arrIn[0];
        for (var el=1; el<arrIn.length; el++) {
            if(arrIn[el] > highest) {
                highest = arrIn[el];
                indices = [];
                indices.push(el);
            } else if ( arrIn[el] == highest) {
                indices.push(el);
            }
       }
       return indices;
   }

答案 2 :(得分:1)

这样可行:

var arr = [1,10,2,10],
    indices = [],
    highest = Math.max.apply(Math, arr);
arr.forEach(function(val, index){
    if (val === highest) indices.push(index);
});
console.log(indices.join(','));

答案 3 :(得分:1)

你可以使用它。

var arr = [1,10,2,10];
var ind = [];
var highValue = Math.max.apply(Math, arr);
arr.map(function(x, i) {
    if (x == highValue) {
    ind.push(i)
  }
});
console.log(ind);//output is [1,3]

答案 4 :(得分:0)

&#13;
&#13;
var highestIndexes = [];
$.each(arr,function(index,item){
   item== Math.max.apply(Math, arr) ? highestIndexes.push(index) : $.noop();
});
console.log(highestIndexes);
&#13;
&#13;
&#13;

试试这个

答案 5 :(得分:0)

仅为了完整性,提案Array#reduce()

&#13;
&#13;
var arr = [1, 10, 2, 10],
    max = arr.reduce(function (r, a, i, aa) {
        if (!i || a > aa[r[0]]) {
            return [i];
        }
        if (a === aa[r[0]]) {
            r.push(i);
        }
        return r;
    }, []);

document.write('<pre>' + JSON.stringify(max, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 6 :(得分:0)

Using underscore/lodash and _.reduce you could write a method to find all indexes based on whatever condition you pass in.

Like:

function findAllIndexes(_arr, condition) {
  return _.reduce(_arr, function(arr, o, index) {
    if(condition(o, _arr)) arr.push(index);
    return arr;
  }, []);
}

console.log(findAllIndexes(nums, function(o, arr){
  return o === Math.max.apply(Math, arr);
}));

This same method can be used with array of Object litetals with whatever condition you give it

Like

var users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'barney',    'active': false },
  { 'user': 'bob', 'active': false }
];

console.log(findAllIndexes(nums, function(o, arr){
  return o.user == 'barney' && o.active == false;     
}));

http://jsbin.com/jadesayicu/edit?html,js,console,output