如何基于另一个对象数组的值创建对象数组?

时间:2018-11-14 13:34:14

标签: javascript arrays

我有8位玩家abcdefghijklmn,{ {1}}的匹配数据存储在对象数组中

op

我想从上述比赛数据中获得顶级球员。顶级球员是获胜率最高的球员

  matchData=[
              {player1:'ab',player2:'cd',winner:'ab',looser:'cd'},
              {player1:'ef',player2:'gh',winner:'gh',looser:'ef'},
              {player1:'ij',player2:'kl',winner:'ij',looser:'kl'},
              {player1:'mn',player2:'op',winner:'mn',looser:'op'},
              {player1:'ab',player2:'cd',winner:'ab',looser:'cd'},
              {player1:'ij',player2:'kl',winner:'kl',looser:'ij'},
              {player1:'ij',player2:'kl',winner:'ij',looser:'kl'}]

并且还希望获得看起来像的对象数组

winrate=(number of matches won/total number of matches played)*100

我该怎么做?

2 个答案:

答案 0 :(得分:-1)

您可以减少输入数据来表示每个玩家的输/赢统计,然后您可以将公式应用于每个玩家的统计以获得所需的输出:

const matchData = [{
    player1: 'ab',
    player2: 'cd',
    winner: 'ab',
    looser: 'cd'
  },
  {
    player1: 'ef',
    player2: 'gh',
    winner: 'gh',
    looser: 'ef'
  },
  {
    player1: 'ij',
    player2: 'kl',
    winner: 'ij',
    looser: 'kl'
  },
  {
    player1: 'mn',
    player2: 'op',
    winner: 'mn',
    looser: 'op'
  },
  {
    player1: 'ab',
    player2: 'cd',
    winner: 'ab',
    looser: 'cd'
  },
  {
    player1: 'ij',
    player2: 'kl',
    winner: 'kl',
    looser: 'ij'
  },
  {
    player1: 'ij',
    player2: 'kl',
    winner: 'ij',
    looser: 'kl'
  }
]

const winRate = ({ lost, won }) => (won / (won + lost)) * 100

const calcStats = results => results.reduce ((out, { winner, looser }) => {
    (out[looser] || (out[looser] = { lost: 0, won: 0 })).lost++ 
    (out[winner] || (out[winner] = { lost: 0, won: 0 })).won++ 
    
    return out
  },
  {}
)


const calcWinRate = stats => 
      Object.entries (stats)
            .map (([ player, stats ]) => ({
                [player]: winRate (stats) 
            }))
            
const output = calcWinRate (calcStats (matchData))

console.log (output)

答案 1 :(得分:-1)

您可以遍历数据并为每个获胜者/失败者创建新数组,如果获胜则添加1,否则获0。然后从新的数组对象计算获胜百分比。

matchData = [
              {player1:'ab',player2:'cd',winner:'ab',looser:'cd'},
              {player1:'ef',player2:'gh',winner:'gh',looser:'ef'},
              {player1:'ij',player2:'kl',winner:'ij',looser:'kl'},
              {player1:'mn',player2:'op',winner:'mn',looser:'op'},
              {player1:'ab',player2:'cd',winner:'ab',looser:'cd'},
              {player1:'ij',player2:'kl',winner:'kl',looser:'ij'},
              {player1:'ij',player2:'kl',winner:'ij',looser:'kl'}
]

let tempRes = {}
for (let d of matchData) {
  let { winner, looser } = d
  
  tempRes[winner] = (tempRes[winner] || []).concat(1)
  tempRes[looser] = (tempRes[looser] || []).concat(0)
  
}

let res = [], topPlayers = [], topValue = 0
for (let [key, value] of Object.entries(tempRes)) {
  let winPercent = (value.reduce((a,b) => a + b) / value.length) * 100
  res.push({ [key]: winPercent })

  winPercent == topValue && (topPlayers.push(key))
  winPercent > topValue && (topValue = winPercent, topPlayers = [key])
}

console.log('Top Player ', topPlayers)
console.log('Result ',res)