Fetch json file with filter

Hi, I want to fetch a json file as suggested by Rich Harris. How can I add a filter to this?

    "imgMain": "israeli-landscapes.jpg",
    "medium": "Oil on canvas",
    "titleEng": "3 Israeli landscapes (l-r) Jordan River, Lake Kinneret, Eilat Mountains.",
    "titleHeb": "נופים ישראלים",
    "width": 40,
    "height": 60,
    "year": "2022",
    "alt": "Israeli landscapes Jordan River, Lake Kinneret, Eilat Mountains. Oil on canvas, Rivka Aderet Myers",
    "imagePos": "margin-left: 0",
    "captionPos": "right: 0"

medium == “Oil on canvas”

for: https://rivka-aderet.art/

Any help much appreciated. -

Just filter the array

const filteredArray = yourArray.filter(entry => entry.medium === 'Oil on canvas');
1 Like

Thanks very much for this Thallius, I’m just starting out with svelte and not sure how to include this with the json fetch, please could you give a snippet where the filter is included with json fetch.

I don’t know svelte, but if it is along the lines of the code you linked to.

Maybe something like this.

export async function preload() {
  const response = await this.fetch('../data/painting.json');
  const paintings = await response.json();
  const oils = paintings.filter(
    (painting) => painting.medium === 'Oil on canvas'

  return {
      paintings: oils

Thanks for this, the return {paintings: oils} gives an “unexpected token” error on oils.
Any ideas?

As mentioned I am not familiar with svelte, so maybe someone else with experience can better comment.

That said, if you read the stackoverflow page you linked to, the OP is running into the same problem.

The most upvoted solution is to write the json file into a static folder

Write the JSON file into the static folder, as static/data/videoslist.json .

Svelte Static Files

Thallius was right, I just added:

    {users = users.filter(user => user.medium === "Oil on canvas") }

It just replaces the users array with a filtered users array no need to change anything else.
Thanks for all your help


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.