In my pursuit to find the optimal performance for each user from a JSON dataset.
const users = [
{
"key": "user1",
"label": "Henry"
},
{
"key": "user2",
"label": "Michael"
},
{
"key": "user3",
"label": "Fiddel"
},
// Additional user data...
]
const results = [
{
"user1": "00:31.241",
"user2": "00:30.945",
// Additional result data...
},
// Additional sets of results...
]
My current solution involves:
let array = [];
const bestTime = results.map(Object.entries).flat().sort((a, b) => a[1] ?.localeCompare(b[1]))[0];
array = [users.filter(user => user.key === test[0])[0].label, test[1]]
The challenge lies in displaying an array containing the best result for every user, not just one. Desired output format: [{name: 'Henry', value: 00:35.233}, {name: 'Michael', value: 00.27.333}, etc]
Currently, I am attempting to calculate the total sum of values for each user, filtering out null entries and computing the sum. However, as the values are stored as strings, converting them to integers for accurate calculation is proving to be a hurdle.
const final = users.map(({ label, key }) => ({
name: label,
value: results.map(r => r[key]).filter(el => el != null).reduce((prev, curr) => parseInt(prev) + parseInt(prev));
}));