Created
October 1, 2018 10:39
-
-
Save devskope/72674beb96f072b966dbffbbf9620077 to your computer and use it in GitHub Desktop.
manipulating an array of ranked objects in JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const randRankObjArray = [ | |
'phil', | |
'bennie', | |
'carmen', | |
'ope', | |
'weaksauce', | |
'amos', | |
'jude', | |
'princess', | |
'some_person', | |
'other_person' | |
].map(name => ({ | |
name, | |
rank: Math.floor(Math.random() * 10 + 1) | |
})); | |
console.log(randRankObjArray); | |
const orderByRanking = rankedObjArray => | |
rankedObjArray.sort((a, b) => (a.rank <= b.rank ? -1 : 1)); | |
const averageRanking = rankedObjArray => { | |
const rankSum = rankedObjArray.reduce( | |
(rankAccum, currentRank) => rankAccum + currentRank.rank, | |
0 | |
); | |
return rankSum / rankedObjArray.length; | |
}; | |
console.log('\n\n ordering by rank... '); | |
console.log(orderByRanking(randRankObjArray)); | |
console.log(`\n Average rank: ${averageRanking(randRankObjArray)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment