简体   繁体   English

在javascript中找到具有常见键的哈希数组的最大值

[英]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). 我目前有一个带有哈希数组的JSON文件,并且希望能够为包含最高“十进制”值的每个匹配键“ Club”返回哈希数组(使用Angular应用程序中的javascript)。

  [
    {"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". 为每个“ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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