Created
June 3, 2019 12:39
-
-
Save Chibuzo/c386ead565b6f6dd9701843236bee6c9 to your computer and use it in GitHub Desktop.
Sort array object using a key
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
let arr = [ | |
{ | |
name: "chibuzo", | |
rank: 3 | |
}, | |
{ | |
name: "John", | |
rank: 1 | |
}, | |
{ | |
name: "Sione", | |
rank: 2 | |
} | |
]; | |
function sortArray(users) { | |
return users.sort((a, b) => parseInt(a.rank) - parseInt(b.rank)); | |
} | |
function findArrayAverage(users) { | |
let sum = 0; | |
users.forEach(user => { | |
sum += user.rank; | |
}); | |
return sum / users.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment