Skip to content

how do I calculate data on collection of game posts #3180

Answered by pdehaan
btrem asked this question in Q&A
Discussion options

You must be logged in to vote

Not sure I'm 100% understanding your setup, but you could create a single filter that takes any arbitrary collection as an argument and returns a "stats" object (which sounds easier than creating n filters; one per stat).

  eleventyConfig.addFilter("gameStats", function (collection = []) {
    return collection.reduce((acc, game) => {
      const { points } = game.data;
      acc.winPercentage = points[0] + points[1];
      acc.streak = acc.streak + 1;
      return acc;
    }, { streak: 0 });
  });

Where I created some fake data with front matter like this:

---
title: Golden State Warriors at Utah Jazz
points:
  - 88
  - 85
winner: Golden State Warriors
---

<h1>{{ title }}</h1>

So verrrr…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@btrem
Comment options

Answer selected by btrem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants