简体   繁体   中英

find highest values for an array of hashes with common keys in javascript

I currently have a JSON file with an array of hashes and would like to be able to return an array of hashes for each matching key "Club" that contains the highest "Decimal" value (using javascript in my Angular app).

  [
    {"Bookmaker": "First", "Club": "Man City", "Decimal": 3.65},
    {"Bookmaker": "First", "Club": "Man Utd", "Decimal": 4.5},
    {"Bookmaker": "First", "Club": "Chelsea", "Decimal": 6.8}
    {"Bookmaker": "Second", "Club": "Man City", "Decimal": 3.5},
    {"Bookmaker": "Second", "Club": "Man Utd", "Decimal": 4},
    {"Bookmaker": "Second", "Club": "Chelsea", "Decimal": 7}
  ]

In this example the returned array would be like so:

[
 {"Bookmaker": "First", "Club": "Man City", "Decimal": 3.65},
 {"Bookmaker": "First", "Club": "Man Utd", "Decimal": 4.5},
 {"Bookmaker": "Second", "Club": "Chelsea", "Decimal": 7}
]

Returning the hashes with the highest "Decimal" value for each "Club".

 var list = [ {"Bookmaker": "First", "Club": "Man City", "Decimal": 3.65}, {"Bookmaker": "First", "Club": "Man Utd", "Decimal": 4.5}, {"Bookmaker": "First", "Club": "Chelsea", "Decimal": 6.8}, {"Bookmaker": "Second", "Club": "Man City", "Decimal": 3.5}, {"Bookmaker": "Second", "Club": "Man Utd", "Decimal": 4}, {"Bookmaker": "Second", "Club": "Chelsea", "Decimal": 7} ]; var filtered = list.reduce(function(p, v){ if(!p[v.Club]){ p[v.Club] = v; } else { if (p[v.Club].Decimal < v.Decimal) { p[v.Club] = v; } } return p; }, {}); //as list filtered = Object.keys(filtered).map(function (key) {return filtered[key]}); console.log(filtered); 

That should work.

You could use a hash table and change the result if a grater value is found in the result set.

 var data = [{ "Bookmaker": "First", "Club": "Man City", "Decimal": 3.65 }, { "Bookmaker": "First", "Club": "Man Utd", "Decimal": 4.5 }, { "Bookmaker": "First", "Club": "Chelsea", "Decimal": 6.8 }, { "Bookmaker": "Second", "Club": "Man City", "Decimal": 3.5 }, { "Bookmaker": "Second", "Club": "Man Utd", "Decimal": 4 }, { "Bookmaker": "Second", "Club": "Chelsea", "Decimal": 7 }], result = []; data.forEach(function (a) { if (a.Club in this) { if (result[this[a.Club]].Decimal < a.Decimal) { result[this[a.Club]] = a; } return; } this[a.Club] = result.push(a) - 1; }, Object.create(null)); console.log(result); 

If you have access to a library like underscore you can simplify the problem to a few chained calls.

var list = [
  {"Bookmaker": "First", "Club": "Man City", "Decimal": 3.65},
  {"Bookmaker": "First", "Club": "Man Utd", "Decimal": 4.5},
  {"Bookmaker": "First", "Club": "Chelsea", "Decimal": 6.8},
  {"Bookmaker": "Second", "Club": "Man City", "Decimal": 3.5},
  {"Bookmaker": "Second", "Club": "Man Utd", "Decimal": 4},
  {"Bookmaker": "Second", "Club": "Chelsea", "Decimal": 7}
];

var maxes = [];

_.chain(list)
  .groupBy(function( obj ) { return obj.Club; })
  .each(function( groupedList ) {        
    maxes.push(_.max(groupedList, function( item ) {
      return item.Decimal;
    }));
  });

// maxes array will now have the three objects with the highest Decimal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM