0

Let me know how to get the highest values for my variable Result in this event for example : if MyFruits ={ "Orange": 1, "Banana": 3, "Apple": 3 }

I would like to get "Banana" and "Apple" but It gives me only the first fruit : "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
 });
Orifjon
  • 915
  • 8
  • 25
bao bao
  • 41
  • 7

6 Answers6

1

Flip your object such that the numbers 1, 3, 3... become keys and their value is an array of corresponding fruits:

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"]
Salman A
  • 262,204
  • 82
  • 430
  • 521
0

Try this:

var max = 0;
var maxProp = null;
for (var property in MyFruits) {
   if (max < MyFruits[property])
   {
      max = MyFruits[property];
      maxProp = property;
   }
}
suvroc
  • 3,058
  • 1
  • 15
  • 29
0

think you have to do two passes ..

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])}

}

array 'results' ends up with array of all that match the highest value

user2808054
  • 1,376
  • 1
  • 11
  • 19
0

Similar question to: How can I order the items in an Object?

You can get the ordered result set like this:

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

The first index res[0] has the biggest object in format

  { key : "Banana", value : 3}

And the second highest in res[1] and so on...

Community
  • 1
  • 1
Tero Tolonen
  • 4,144
  • 4
  • 27
  • 32
0

You need to keep elements if they are same. You can use temporary array for this.

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;
            })
Orifjon
  • 915
  • 8
  • 25
0

User .filter() instead of .reduce().

Here is the example:

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

Otherwise your condition does great work! Happy Coding...!!

Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27