SpamFatFree
SpamFatFree

Reputation: 1

ES6 Processing Objects (Reduce Functions)

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

Upvotes: 0

Views: 114

Answers (5)

arizafar
arizafar

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

Lafi
Lafi

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

Nina Scholz
Nina Scholz

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

Jeto
Jeto

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

vahdet
vahdet

Reputation: 6729

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

Upvotes: 1

Related Questions