Skip to content

Syntax support #6610

Closed
Closed
@tanekim88

Description

@tanekim88

Describe the problem

<script>
 const obj = getObject();
// obj can be { items: [1,2,3]}; or { items: undefined};
 const lookup = {
1: {
valid: true,
value:'good'
},
2:{
valid: true,
value:'bad'
},
3:{
valid: false,
value:'soso'
}
}
</script>
{#if obj.items}
     {#each obj.items as item}
// Here, I wish I could just do  {#each obj?.items as item}
// Instead of having to wrap with if block to
// check if obj.items is not undefined.
// Currently, it errors out if obj.items is null without the #if block

           {#if lookup[item].valid}
// Here I want to declare a variable:
// const lookupItem = lookup[item]; so that I can use 
//lookupItem.value instead of lookup[item].value in the body.
// Is there way to do so?
// I wish I could declare it when using if block
// {#if lookup[item] as lookupItem}
// or it would be also good if I could use local block like
// {# const lookupItem = lookup[item]; /}
                 <span>lookup[item].value</span>
           {/if}
     {/each}
{#if}

Currently, my attempt to use Svelte for my current project looks something like this (note that the complicated Array.from(...) equations could be avoided if there was a way to declare a variable inside the parent's each block and do calculations to get range value and store inside the variable.):
image

In Angular, there is a way to declare a variable through the if block,
https://angular.io/api/common/NgIf
image

I wish svelte could provide a solution like this.

Describe the proposed solution

I already described in the comments

Alternatives considered

I already described in the comments

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions