Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

each for new Set([]) #3225

Closed
frederikhors opened this issue Jul 11, 2019 · 7 comments
Closed

each for new Set([]) #3225

frederikhors opened this issue Jul 11, 2019 · 7 comments

Comments

@frederikhors
Copy link

Is it possible to iterate over new Set([]) with e.g. {#each ...}?

What is the recommended method?

@Conduitry
Copy link
Member

Spread it into an array with [...set]. Adding iterable support to {#each} has come up several times. See #894 for why we we're not excited about the idea.

@frederikhors
Copy link
Author

Thanks @Conduitry but I think I explained myself wrong.

What I'd like to do is something like:

let selectedPlayers = new Set()

function addOrRemove(playerID) {
  if (selectedPlayers.has(playerID)) {
    selectedPlayers.delete(playerID)
  } else {
    selectedPlayers.add(playerID)
  }
}

I don't know how to use it with:

{#if selectedPlayers.size}
  {#each selectedPlayers as player}
    {player}
  {/each}
{/if}

@pngwn
Copy link
Member

pngwn commented Jul 12, 2019

The same approach will work:

{#if selectedPlayers.size}
  {#each [ ...selectedPlayers ] as player}
    {player}
  {/each}
{/if}

@frederikhors
Copy link
Author

@pngwn
Copy link
Member

pngwn commented Jul 12, 2019

You need to follow the reactivity rules, there is no assignment here so an update will not be triggered.
https://svelte.dev/repl/0dca50469e8e43b2835e655324a7ade0?version=3.6.7

@frederikhors
Copy link
Author

Oh. Thanks.

selectedPlayers.size > 0 is the same as selectedPlayers.size, right?

@pngwn
Copy link
Member

pngwn commented Jul 12, 2019

Has the same effect, yeah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants