Closed
Description
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.):
In Angular, there is a way to declare a variable through the if block,
https://angular.io/api/common/NgIf
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
Labels
No labels