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

Index variables with nested {{#each}} loops #126

Closed
Conduitry opened this issue Dec 5, 2016 · 3 comments
Closed

Index variables with nested {{#each}} loops #126

Conduitry opened this issue Dec 5, 2016 · 3 comments

Comments

@Conduitry
Copy link
Member

https://svelte.technology/repl/?gist=a7fd73504362ea13fadcdb507c6e2c9a

Component:

{{#each array as row, i}}
{{#each row as cell, j}}
[ {{i}}, {{j}} ]
{{/each}}
<br>
{{/each}}

Data:

{
  "array": [[0,0,0],[0,0,0],[0,0,0]]
}

What I'm getting:

[ 0, 0 ][ 1, 1 ][ 2, 2 ] 
[ 0, 0 ][ 1, 1 ][ 2, 2 ] 
[ 0, 0 ][ 1, 1 ][ 2, 2 ] 

What I would expect to be getting:

[ 0, 0 ][ 0, 1 ][ 0, 2 ] 
[ 1, 0 ][ 1, 1 ][ 1, 2 ] 
[ 2, 0 ][ 2, 1 ][ 2, 2 ] 

It looks like the {{#each}} index variable i is getting overwritten by the inner loop. Am I doing something wrong here or is this a bug / known current limitation?

@Rich-Harris
Copy link
Member

Rich-Harris commented Dec 5, 2016

This is related to #88 – basically, Svelte isn't deconflicting variable names, which it needs to. It's using i internally to iterate. If you use e.g. j and k instead of i and j it should work around the issue pending a proper fix.

Demo: https://svelte.technology/repl/?gist=2eff688ccdaed9886f92e92d1145216f

@Conduitry
Copy link
Member Author

Ah okay, I guess I just got lucky by using i when I was fiddling with this :) It does work when I use different variable names. Thanks for the super quick response!

Rich-Harris added a commit that referenced this issue Dec 6, 2016
@Rich-Harris
Copy link
Member

Fixed in 1.1.2

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

2 participants