You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's a useful pattern you can use while destructuring in normal JS, which is that within the destructure you can reference previously-extracted properties.
consto={one : 1};// Here's where the exciting stuff happens// v-----------vconst{ one, two =one+1}=o;console.log(one,two);
I wanted to use (abuse?) this functionality to avoid having to create an entire array or modify an array that I was passed, because allocating a whole array for something that can be easily calculated on a per-element basis while iterating didn't seem worth it.
functionget_each_context(ctx,list,i){constchild_ctx=ctx.slice();child_ctx[1]=list[i].one;// This reference to one isn't rewritten to be child_ctx[1], so one is undefined// v---------vchild_ctx[2]=list[i].two!==undefined ? list[i].two : add(one,1);returnchild_ctx;}
Expected behavior 1 - 2 is logged
Information about your Svelte project:
Firefox latest, but it's in the generated code so happens everywhere
Severity
It mostly means that if you need to look up data to make decisions inside an {#each} you either need to modify the array elements or create a second, shadow array based on the elements containing the extra data. This is all doable but I'd much prefer not to if possible. Since templates can't declare new variables I haven't been able to come up with an alternative that lets it be handled entirely in template code.
The text was updated successfully, but these errors were encountered:
Describe the bug
There's a useful pattern you can use while destructuring in normal JS, which is that within the destructure you can reference previously-extracted properties.
Live Example
I wanted to use (abuse?) this functionality to avoid having to create an entire array or modify an array that I was passed, because allocating a whole array for something that can be easily calculated on a per-element basis while iterating didn't seem worth it.
To Reproduce
https://svelte.dev/repl/0ee7227e1b45465b9b47d7a5ae2d1252?version=3.23.2
Here's the problematic generated code:
Expected behavior
1 - 2
is loggedInformation about your Svelte project:
[email protected]
Severity
It mostly means that if you need to look up data to make decisions inside an
{#each}
you either need to modify the array elements or create a second, shadow array based on the elements containing the extra data. This is all doable but I'd much prefer not to if possible. Since templates can't declare new variables I haven't been able to come up with an alternative that lets it be handled entirely in template code.The text was updated successfully, but these errors were encountered: