Introduction
Theeach block helper is used to iterate over an array or object. It is similar to the for loop in other programming languages.
Basic usage
To use theeach block helper, you need to pass an array or object to it. The each block helper will then iterate over the array or object and execute the block for each item.
each block helper iterates over the array [1, 2, 3] and executes the block for each item. The block is passed the current item as an argument, which is then used to render the paragraph element.
Iterating over query results
Theeach block helper is often used to iterate over the results of a query. For example, you might want to display a list of blog posts, or a table of users. Since we have to run the query first, similar to the if block helper, we can use the await block helper to wait for the query to complete before rendering the results.
await block helper to wait for the runQuery function to complete. Once the query is complete, the users variable will contain the results, and we can use the each block helper to iterate over the results and render a Card component for each user.
Learn more
To learn more about theeach block helper, check out the official Svelte documentation.