javascript - Get Fruits with Highest value in object -
let me know how highest values variable result
in event example : if myfruits ={ "orange": 1, "banana": 3, "apple": 3 }
i "banana"
, "apple"
gives me first fruit : "banana"
// first variable myfruits must have index 0 // , after user clicks 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] ? : b });
you need keep elements if same. can use temporary array 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] ? : b; resultarray = []; resultarray.push(bigger); return bigger; })
Comments
Post a Comment