Phong Duong


Each block in Svelte

11-08-2020 | #svelte

To iterate array in Svelte, you use each block

	let cats = [
		{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
		{ id: 'z_AbfPXTKms', name: 'Maru' },
		{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }

{#each cats as cat, i}
	<li><a target="_blank" href="{}">
		{i + 1}: {}

You can use destruction normally. In case you need to pass key to the elements inside each block, you can use any type of value as key even object

{#each things as thing (}
	<Thing current={thing.color}/>

Share: TwitterFacebookLinkedInHacker News