admin管理员组文章数量:1389768
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 badges2 Answers
Reset to default 5I'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"}
本文标签: javascriptIndex of the largest float in the arrayStack Overflow
版权声明:本文标题:javascript - Index of the largest float in the array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744729626a2621971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论