I need help accessing specific objects in a JSON file based on a name value in Svelte.
I am using JSON as a data source and am able to pull it into my page and loop through it with each.
The JSON looks like so:
[
{
"id": 1,
"title": "Project 1",
"body": "Project 1 text"
},
{
"id": 2,
"title": "Project 2",
"body": "Project 2 text"
}
]
I access the JSON like this:
<script context="module">
export const load = async ({ fetch }) => {
const res = await fetch("https://amarton.github.io/amcom-portfolio-svelte/static/port.json");
const projects = await res.json();
return {
props: {
projects,
}
}
}
</script>
<script>
export let projects;
</script>
And then loop through and display it like this:
{#each projects as project}
<h2>{project.title}</h2>
<p>{project.body}</p>
{/each}
I'd like to be able to access objects based on my assigned ids. For example, how do I grab JUST the object with the id of 2 and display its title and body in my page?
Sorry if this is a very basic question and thanks for much for any help you can provide.