我可以在 Svelte 中创建一个比较每个块来保留我的索引吗? [英] Can I create a comparative each block in Svelte to retain my index?

查看:25
本文介绍了我可以在 Svelte 中创建一个比较每个块来保留我的索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有更有效的方法来使用每个块.现在索引不能像我想要的那样工作,因为整个答案在一些被删除之前就得到了它们的索引.

{#each 作为答案,i}{#if poll.uuid == answer.pollid}<div class="answer";on:click={vote(answer.uuid)}><div class="persentage";样式=宽度:{getprosent(poll.uuid, i)}%"></div><span>{answer.answer}</span><div class="votes"><span>{votes.filter(vote =>vote.pollid == answer.uuid).length}</span>

{/如果}{/每个}

有没有什么办法可以像上面看到的那样进行比较,而不用让整个数组获得索引?

解决方案

您可以过滤answers,例如:

const pollAnswers = answers.filter(answer => poll.uuid == answer.pollid){#each pollAnswers 作为答案,i}

I'm wondering if there is a more efficient way to use this each block. Right now the index does not work as I want it to because the entire answers get their index before some get removed.

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

Is there any way I can compare like seen above without the whole array getting an index?

解决方案

You can filter the answers, like:

const pollAnswers = answers.filter(answer => poll.uuid == answer.pollid)

{#each pollAnswers as answer, i}

这篇关于我可以在 Svelte 中创建一个比较每个块来保留我的索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆