我有几个变量并分配了一些值。例如:
增益权重 = 0.01 增益天气 = 0.02
还有一些类似的。现在我想获取分配最高值的变量名称。
当我做这样的事情时,它返回值而不是名称。
var rootNode = Math.max(Gain_weight,Gain_smoking,Gain_exercising,Gain_foodhabits,Gain_parent_heartattack,
Gain_alcohol,Gain_occupation,Gain_workinghrs);
那么除了值之外,我如何获得分配的最高值字符串?
最佳答案
检查后question看来这个名字可能无法得到。
解决方法可以是使用对象
或关联数组,其名称与变量名称相同,其值与变量值相同
希望这段代码有助于理解
// Array with name value pair
var _myDemoArray = [
{name:'Gain_weight',
value:0.01},
{name:'Gain_weather',
value:0.02}]
// sort the array in descending order to get the element with maximum value.
var _getSortedElem = _myDemoArray.sort(function(a,b){
return b.value -a.value;
});
// The first element of this sorted array will have element with maximum value
console.log(_getSortedElem[0].name);
检查这个jsfiddle用于演示
关于javascript - Angularjs获取具有最高值的变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383945/