I came across a JSON structure that looks like the following:
{
"user": [
{"username": "x1", "pfp": "", "scores": [{"easy": 10, "normal": 1, "hard": 2, "oni": 3, "uraoni": 4}]},
{"username": "x2", "pfp": "", "scores": [{"easy": 3, "normal": 1, "hard": 2, "oni": 3, "uraoni": 4}]},
{"username": "x3", "pfp": "", "scores": [{"easy": 5, "normal": 1, "hard": 2, "oni": 3, "uraoni": 4}]},
{"username": "x4", "pfp": "", "scores": [{"easy": 0, "normal": 40, "hard": 2, "oni": 3, "uraoni": 4}]}
]
}
The desired result when ordered by username should be: x4, x1, x3, x2
(x4 with a total of 49, x1 with 20, x2 with 15, and x3 with 13).
I am looking to use the map() function to organize the array based on the sum of the scores.
I attempted to use map for reducing the scores and then sorting it, but encountered difficulties with the reduce() method.
let userscopy = userjson
userscopy.map((user) => (
user.scores[0] = JSON.parse(user.scores).reduce((a, b) => a + b)
))
https://i.stack.imgur.com/eoWRN.png Any thoughts or suggestions?