您能否让索引只计算通过 Svelte 中的 if 语句的数据?

Can you make the index only count for data that pass through an if statement in Svelte?

我的问题措辞不当,但我会再试一次。

{#each answers as answer, index}
{#if poll.uuid == answer.pollid}
        <div class="answer" on:click={vote(answer.uuid)}>
            <div class="persentage" style="width: {getprosent(poll.uuid, index)}%"></div>
            <span>{answer.answer}</span>
            <div class="votes">
            <span>{votes.filter(vote => vote.pollid == answer.uuid).length}</span>  
            </div>
        </div>
{/if}
{/each}

基本上我只想使用“每个”的索引,但只使用通过该 if 语句的索引。所以在我的代码中我有不同的民意调查,每一个民意调查都有答案。我只想显示与投票具有 uuid 的 pollid 相同的答案。这适用于显示正确民意调查的正确答案。尽管我只想访问显示的答案的索引。现在,对于每个民意调查,都会给出相同的索引,并且会给出未显示的答案的索引。

我是新手,我觉得这很愚蠢,但有没有办法让我给出正确的 0、1、2、3,但只给出通过 if 语句的答案。

让我把我的评论写出来。您可以执行以下操作:

{#each answers.filter((ans) => poll.uuid === ans.pollid) as answer, index}
        <div class="answer" on:click={vote(answer.uuid)}>
            <div class="persentage" style="width: {getprosent(poll.uuid, index)}%"></div>
            <span>{answer.answer}</span>
            <div class="votes">
            <span>{votes.filter(vote => vote.pollid == answer.uuid).length}</span>  
            </div>
        </div>
{/each}