最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Index of the largest float in the array - Stack Overflow

programmeradmin0浏览0评论

How can I get the index of the largest element in the array of floats?

[0.000004619778924223204, 0.8323721355744392, 0.9573732678543363, 1.2476616422122455e-14, 2.846605856163335e-8]

Once I get this index, I then want to get the value of another array at this index.

Let's call the second array:

['a', 'b', 'c', 'd', 'e']

When I ran the following, I got 'b' instead of 'c'.

I iterated through the first array and made a map of the floats to the strings. Then I got the string at the the first element of the first array ( array of floats ) sorted.

//aInput in the array of floats.
var emObj={};
for(var i=0; i<aInput.length; i++){
    emObj[aInput[i]]=['a', 'b', 'c', 'd', 'e'][i];
}
return emObj[aInput.sort()[0]];

I also tried a method where I iterated through the array of floats and stored the largest value in a variable. Then I'd do something like this:

return ['a', 'b', 'c', 'd', 'e'][aInput.indexOf(largestFloat)];

But neither of these worked. Always returning the wrong string.

How can I get the index of the largest element in the array of floats?

[0.000004619778924223204, 0.8323721355744392, 0.9573732678543363, 1.2476616422122455e-14, 2.846605856163335e-8]

Once I get this index, I then want to get the value of another array at this index.

Let's call the second array:

['a', 'b', 'c', 'd', 'e']

When I ran the following, I got 'b' instead of 'c'.

I iterated through the first array and made a map of the floats to the strings. Then I got the string at the the first element of the first array ( array of floats ) sorted.

//aInput in the array of floats.
var emObj={};
for(var i=0; i<aInput.length; i++){
    emObj[aInput[i]]=['a', 'b', 'c', 'd', 'e'][i];
}
return emObj[aInput.sort()[0]];

I also tried a method where I iterated through the array of floats and stored the largest value in a variable. Then I'd do something like this:

return ['a', 'b', 'c', 'd', 'e'][aInput.indexOf(largestFloat)];

But neither of these worked. Always returning the wrong string.

Share Improve this question asked Jun 6, 2015 at 23:25 wordSmithwordSmith 3,1839 gold badges33 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I'd suggest using Math.max() and indexOf()

var maxNum = Math.max.apply(null, aInput);
var index = aInput.indexOf(maxNum);
return ['a', 'b', 'c', 'd', 'e'][index];

An approach using Array.prototype.reduce()

This allows for arbitrary plexity in the reduce logic.

First, set up some data:

  var data = [0.000004619778924223204, 0.8323721355744392, 0.9573732678543363, 1.2476616422122455e-14, 2.846605856163335e-8];
  var reference = ['a', 'b', 'c', 'd', 'e'];

Then, find the value and index of the maximum value in the array.

  var result = data.reduce(function (previousValue, currentValue, index, array) {
    if(previousValue.value > currentValue) {
      return previousValue;
    } else {
      return {value: currentValue, index: index};
    }
  })
  console.log(reference[result.index]);

Alternatively you could find the referenced value directly like this.

var result = data.reduce(function (previousValue, currentValue, index, array) {
  if(previousValue.value1 > currentValue) {
    return previousValue;
  } else {
    return {value1: currentValue, value2: reference[index]};
  }
})
console.log(result);

This outputs Object {value1: 0.9573732678543363, value2: "c"}

发布评论

评论列表(0)

  1. 暂无评论