0

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:

  1. Iterate through all objects
  2. Use global variable to count matches (m) and tries (y)
  3. Return m, y respectively

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

5 Answers 5

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

You should use reduce on fields as such:

function countStats (list) {
  return {
    matches: input.reduce((a, b) => +a + +b.matches, 0),
    tries: input.reduce((a, b) => +a + +b.tries, 0)
  }
}

See the syntactic sugar to sum strings as numbers i.e. +a + +b

2 Comments

Thanks this is the format I had in mind. Can you please break down what the parameters a, b refer to? And what the ending 0 is?
a and b are variables for that inline, anonymous function. Their names are arbitrary indeed. And the zeroes are the offset values representing the case that the initial value of our operation (sum) is zero. It can be omitted generally, however for this specific case, it is required as otherwise we would start off with a string which would mess up the result.
0

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);

Comments

0

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);

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.