在对象中获得具有最高价值的水果

时间:2015-05-11 13:45:29

标签: javascript object

让我知道如何在此事件中获取变量Result最高值,例如: if MyFruits ={ "Orange": 1, "Banana": 3, "Apple": 3 }

我想获得"Banana""Apple",但它只给了我第一个水果:"Banana"

 // first the variable MyFruits must have index 0
 // and after the user clicks to give value.
 var MyFruits = {
     "Orange": 0,
     "Banana": 0,
     "Apple": 0
 };
 $('input:checked').each(function(index) {
     var key = $(this).val();
     if (key in MyFruits) {
         MyFruits[key]++;
     } else {
         MyFruits[key] = 1;
     }
 });
 var Result = Object.keys(MyFruits).reduce(function(a, b) {
     return MyFruits[a] > MyFruits[b] ? a : b
 });

6 个答案:

答案 0 :(得分:1)

翻转你的物体,使数字1,3,3 ......成为键,它们的值是一系列相应的果实:

var MyFruits = { "Orange": 1, "Banana": 3, "Apple": 3 };
var FruitsValue = {};
var MaxValue = -1;
$.each(MyFruits, function(friut, value) {
    if (FruitsValue[value] === undefined) {
        FruitsValue[value] = [];
    }
    FruitsValue[value].push(friut)
    MaxValue = Math.max(MaxValue, value);
});
console.log(FruitsValue);
// {1: ["Orange"], 3:["Banana","Apple"]}
console.log(FruitsValue[MaxValue]);
// ["Banana", "Apple"]

答案 1 :(得分:0)

试试这个:

var max = 0;
var maxProp = null;
for (var property in MyFruits) {
   if (max < MyFruits[property])
   {
      max = MyFruits[property];
      maxProp = property;
   }
}

答案 2 :(得分:0)

认为你必须做两次通过..

var MyFruits ={ "Orange": 1, "Banana": 3, "Apple": 3 };
var highest=0;
var results = [];
//pass 1 : find highest
for (f in MyFruits)
{
    if (MyFruits[f] > highest) { highest = MyFruits[f] ) }
}

// pass 2 : find all which match highest
for (f in MyFruits[f]) {
  if (MyFruits[f] == highest) {results.push(MyFruits[f])}

}

阵列&#39;结果&#39;最终得到所有匹配最高值的数组

答案 3 :(得分:0)

类似的问题:How can I order the items in an Object?

您可以像这样获得有序的结果集:

var res = Object.keys(MyFruits).map(function(key ) {
  return { key: key, value : dict[key] };
}).sort(function(a, b) { 
   return b.value - a.value
});

第一个索引res [0]的格式最大的对象是

  { key : "Banana", value : 3}

res [1]中的第二高等等......

答案 4 :(得分:0)

如果元素相同,则需要保留元素。您可以使用临时数组。

var MyFruits ={ "Orange": 1, "Banana": 3, "Apple": 3 };
                $('input:checked').each(function(index) {
                    var key = $(this).val();
                    if (key in MyFruits) {
                        MyFruits[key]++;
                    } else {
                        MyFruits[key] = 1;
                    }
                });


var resultArray = [];
var Result = Object.keys(MyFruits).reduce(function(a, b) {
                if( MyFruits[a] == MyFruits[b] ) {
                    resultArray.push(b);
                    return a;  
                }

                 var bigger = MyFruits[a] > MyFruits[b] ? a : b;
                 resultArray = [];
                 resultArray.push(bigger);
                 return bigger;
            })

答案 5 :(得分:0)

用户.filter()代替.reduce()

以下是示例:

Object.keys(MyFruits).filter(function (a, b) {
    return MyFruits[a] > MyFruits[b] ? a : b;
});

否则你的病情很棒!快乐的编码...... !!