How to create an array of objects based on values of another array of objects?

Arjun sr

I have 8 players ab,cd,ef,gh,ij,kl,mn,op their match data is stored in an array of objects

  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'}]

I want to get the top player from the above match data.The Top player is the one who is having the highest win rate

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

And also want to get the array of objects which look like,

PlayerData=[{ab:87},{cd:78},{ef:57},{gh:75},{ij:47},{kl:14},{mn:65},{op:44}]

How can I do this?

Nitish Narang

You can loop though data and create new array for each winner/looser adding in 1 if wins else 0 if looses. Then calculate percentage of wins from new array object.

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to create an array of objects based on another array of objects?

Filter array of objects based on values in another array

Angular - filter an array of objects based on values in another array of objects

create an array of objects based on another array of objects programmatically

How to reduce an array of objects inside an array of objects based on another array

How to arrange an array with objects based on another array?

How to split an Array of Objects into another Arrays based on their values?

Filtering an array based off of another objects values

How to create array of objects from keys and values of another object

Create an array of objects with values from another object

How to create a new array of objects based on the values of two existing arrays?

How do I create new arrays of objects with objects from an existing array, based on values in the objects?

How to lookup an array of objects based upon another array of objects?

How to change the objects of an array based on objects of another array

Remove values in nested array of objects based on values from another array

Create array of objects based by filtering the data based on another array

How to get values from one array of objects into another array of objects

Filter array of objects based on another array of objects

Creating array of objects based on another array of objects

filter an array of objects based on another array of objects

How to create a new array using the values of an array of objects within objects?

Create an array of objects recursively based in array of objects

How to sort an array of objects based on another?

Remove objects from an Array based on values in another array

filtering an array of objects based on another array of values, returning an empty list

javascript create array of objects with a fixed length based on another bigger array

How to create a type, based on the properties of objects in an array?

Create or reorder an array based on values from array of objects

How to create array of JSON objects with multiple values?