Getting max value(s) in JSON array

前端 未结 11 1673
醉梦人生
醉梦人生 2020-12-06 05:56

I\'m trying to create a JavaScript function which takes information from an array in an external JSON and then takes the max value (or the top 5 values) for one of the JSON

相关标签:
11条回答
  • 2020-12-06 06:35

    This will allow you to choose what stat you want and what information you want back.

    http://jsbin.com/tudegofa/1/edit

    data => is the array

    stat => is the stat you want to sort by

    info => is an array of properties you want returned.

    function getValues (data, stat, info)
    {
      var selectedValues = data.map(function(x) {
        return parseFloat(x[stat]);
      })
    
      var i = selectedValues.indexOf(Math.max.apply(Math, selectedValues));
    
      var result = {};
      info.forEach(function(x) {
          result[x] = test[i][x];
      })
      return result;
    }
    
    var myData = '';
    $.getJSON('/url/to/grab/json', function(data) {
    
      myData = data;
    
    });
    
    getValues(myData, "bpg", ["player","team"]);
    
    //[object Object] {
    //  player: "Anthony Davis",
    //  team: "New Orleans Pelicans"
    // }
    
    0 讨论(0)
  • 2020-12-06 06:38

    My solution here. Remember to use == instead of === to compare a number with a string.

    const getMax = (arr, prop) => {
      const tmp = arr.map(x => x[prop]);
      const max = Math.max(...tmp);
      return arr.filter(x => x[prop] == max);
    }
    
    getMax(myArr,"bpg")
    

    One line version:

    myArr.filter( x => x["bpg"] == Math.max(...myArr.map(x => x["bpg"])) )
    
    0 讨论(0)
  • 2020-12-06 06:39

    I found the following approach very neat:

    arr.sort( 
      function(a, b) {
         return parseFloat(b['ppg']) - parseFloat(a['ppg']);
      }
    )[0]['player']
    

    Demo in snippet:

    var arr =[
    {
        "player" : "Andre Drummond",
        "team" : "Detroit Pistons",
        "ppg" : "15.4",
        "rpg" : "11.6",
        "apg" : "2.4",
        "bpg" : "1.6",
        "spg" : "0.8",
        "3pg" : "0.1"
    },
    {
        "player" : "Anthony Davis",
        "team" : "New Orleans Pelicans",
        "ppg" : "16.4",
        "rpg" : "13.6",
        "apg" : "2.6",
        "bpg" : "3.5",
        "spg" : "1.2",
        "3pg" : "0.1"
    },
    {
        "player" : "Carmelo Anthony",
        "team" : "New York Knicks",
        "ppg" : "27.4",
        "rpg" : "5.4",
        "apg" : "4.5",
        "bpg" : "1.1",
        "spg" : "1.5",
        "3pg" : "1.6"
    }
    ]
    console.log(
    arr.sort( 
        function(a, b) {
           return parseFloat(b['ppg']) - parseFloat(a['ppg']);
        }
        )[0]['player']
    );

    First, I sort the array in descending order, then I choose the first element which contains the max value. In the code, I found the player who has the max ppg value. Hope this helps!

    0 讨论(0)
  • 2020-12-06 06:39

    Simplicity thanks you in the long run.

    function getMaxValueByAttribute(arr, attr) {
        var max = "-99999999999";
        arr.forEach(function (member, index) {
                // console.log(member, index);
                if (member.hasOwnProperty(attr) && parseFloat(member[attr]) > parseFloat(max)) {
                    max = member[attr];
                    // console.log("Max now: " + max);
                }
            });
        return max;
        }
    

    Then use it like:

    var result = getMaxValueByAttribute(arr, "ppg");
    // result = "27.4"
    
    0 讨论(0)
  • 2020-12-06 06:40

    the more simple:

    const players =                                           
      [ { player: 'Andre Drummond',  team: 'Detroit Pistons',      ppg: '15.4', rpg: '11.6', apg: '2.4', bpg: '1.6', spg: '0.8', '3pg': '0.1' } 
      , { player: 'Anthony Davis',   team: 'New Orleans Pelicans', ppg: '16.4', rpg: '13.6', apg: '2.6', bpg: '3.5', spg: '1.2', '3pg': '0.1' } 
      , { player: 'Carmelo Anthony', team: 'New York Knicks',      ppg: '27.4', rpg: '5.4',  apg: '4.5', bpg: '1.1', spg: '1.5', '3pg': '1.6' } 
      ] 
    
    const getPlayerMax_on = cod => players.reduce((a,c)=>((+a[cod])<(+c[cod]))?c:a)
    
    const maxOn_ppg = getPlayerMax_on('ppg')
    
    console.log( maxOn_ppg.player, maxOn_ppg.team, maxOn_ppg.ppg )

    0 讨论(0)
提交回复
热议问题