Skip to content

Instantly share code, notes, and snippets.

@r2dev2
Created July 31, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r2dev2/a01e185cd06e36efc574c2935c72ecdf to your computer and use it in GitHub Desktop.
Save r2dev2/a01e185cd06e36efc574c2935c72ecdf to your computer and use it in GitHub Desktop.
Changing element of reactive array svelte
<script>
let messages = [
{ text: 'bru' },
{ text: 'moment' },
{ text: 'testing' },
{ text: '123' },
];
let hideChoice = '';
const hide = () => {
const i = parseInt(hideChoice);
const before = messages.slice(0, i);
const after = messages.slice(i + 1);
messages = [...before, { ...messages[i], text: 'hidden' }, ...after];
};
</script>
<input bind:value={hideChoice} />
<button on:click={hide}>
Hide
</button>
{#each messages as msg, i}
<p>{msg.text}</p>
{/each}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment