Reputation: 1
Greetings from Romania,
Given a list of games, which are objects that look like:
{
"id": 112814,
"matches": "123",
"tries": "11"
}
return a object like such
{
"totalMatches": m,
"totalTries": y
}
Where m is the sum of all matches for all games and t is the sum of all tries for all games.
input = [
{"id": 1,"matches": "123","tries": "11"},
{"id": 2,"matches": "1","tries": "1"},
{"id": 3,"matches": "2","tries": "5"}
]
output = {
matches: 126,
tries: 17
}
What I currently have:
function countStats(list) {
return {
matches: 0,
tries: 0
};
}
What I think I need to do:
What I was told to do instead, which I do not know how: 1. Use the reduce function 2. Return an object instead
Many thanks for any advice
Upvotes: 0
Views: 114
Reputation: 3122
input = [
{"id": 1,"matches": "123","tries": "11"},
{"id": 2,"matches": "1","tries": "1"},
{"id": 3,"matches": "2","tries": "5"}
]
let totalMatches = 0;
let totalTries = 0;
let output = input.reduce(({totalMatches, totalTries}, {matches, tries}) => {
totalMatches += +matches;
totalTries += +tries;
return {totalMatches, totalTries}
},{totalMatches: 0, totalTries: 0});
console.log(output)
Upvotes: 0
Reputation: 1342
here is a clean way of doing it:
const games = [
{ id: 1, matches: '123', tries: '11' },
{ id: 2, matches: '1', tries: '1' },
{ id: 3, matches: '2', tries: '5' },
];
const reduced = games.reduce(
(accumulator, game) => ({
totalMatches: accumulator.totalMatches + Number(game.matches),
totalTries: accumulator.totalTries + Number(game.tries),
}),
{ totalMatches: 0, totalTries: 0 }
);
console.log(reduced);
Upvotes: 0
Reputation: 386560
You could take an object for the result with zero values and take the keys for later iterating the array and the keys for adding same key values.
var input = [{ id: 1, matches: "123", tries: "11" }, { id: 2, matches: "1", tries: "1" }, { id: 3, matches: "2", tries: "5" }],
output = { matches: 0, tries: 0 },
keys = Object.keys(output);
input.forEach(o => keys.forEach(k => output[k] += +o[k]));
console.log(output);
Upvotes: 0
Reputation: 14927
You can make use of Array.reduce
:
const input = [
{"id": 1, "matches": "123", "tries": "11"},
{"id": 2, "matches": "1", "tries": "1"},
{"id": 3, "matches": "2", "tries": "5"}
];
const result = input.reduce((result, entry) => {
result.matches += +entry.matches;
result.tries += +entry.tries;
return result;
}, {matches: 0, tries: 0});
console.log(result);
Upvotes: 2