DataviewJS Snippet Showcase

Hello.

In the past I used an exemplar created by someone online to log my takeaway food entries in the YAML of my daily notes.

---
date: 2012-09-19
purchases:
  - store: "[[some store]]"
    item: some item
    comments: comments
  - store: "[[another store]]"
    item: another item
    comments: other comments
---
---
date: 2012-09-21
purchases:
  - store: "[[another store]]"
    item: potatoes, potatoes
  - store: "[[another store]]"
    item: a very big watermelon
---
---
date: 2012-09-20
purchases:
  - store: "[[some store]]"
    item: apples
  - store: "[[some store]]"
    item: oranges
---
// 1. Let's gather all relevant pages

const pages =
  dv.pages('"_tmp/20210922 test for sumant28/Life"')
    .filter(page => page.file.name.includes("test note"));

// 2. Now let's transform from thing per date (we have single page for each date) into thing per purchase (we want to have as many rows in a table as all purchases extracted from all pages)

const purchases = [];

pages.forEach(page => {
  page.purchases.forEach(purchase => {
    purchases.push({
      file: page.file,
      date: page.date,
      store: purchase.store,
      item: purchase.item,
      comments: purchase.comments,
    });
  });
});

// 3. Let's sort it by date, newest first (note "-" before "purchase.date")

purchases.sort(purchase => -purchase.date);

// 4. And let's print a table of those purchases

dv.table(
  [
    "File",
    "store",
    "item",
  ],
  purchases.map(purchase => [
    purchase.file.link,
    purchase.store,
    purchase.item,
  ])
 );

I had logged dozens of entries making the resulting table too unwieldy. I already have pages set up for each store in Obsidian which is a file link in the table using this syntax “[[store]]”. I want to display a filtered DataviewJS with purchases only from that store that goes on the bottom of every store file in my vault. I figured out how to filter the table using an extra int field called rating but I want to filter instead based on the .includes function but whenever I try to do this I get the error message saying purchases.store.includes is not a function.

2 Likes