Search code examples
svelte

Change the increment in Svelte's #each logic block


Is it possible to change the increment in the #each logic block?

Using a for loop, it's simple to change the increment to 2, for example:

for (let i = 0; i < items.length; i+=2) { 

I get an error when trying to do the same within Svelte's #each block.


Solution

  • It's not possible.

    You could either (reactively if necessary) create a new array from the existing one or skip indices you don't want to display via #if.

    {#each items as item, i}
      {#if i % 2 == 0}
         <!-- every second item -->
      {/if}
    {/each}