gpt4 book ai didi

javascript - 在 Meteor 中使用#each,检查集合中的 'last' 元素是否到达

转载 作者:数据小太阳 更新时间:2023-10-29 04:06:06 25 4
gpt4 key购买 nike

我正在使用 {{#each}} 遍历 Meteor 中的一个集合,我想知道我是否在最后一个元素中,就像我在 AngularJS 中使用带有 $last 的 ngRepeat 时所做的那样。

它可以用来构建人类可读的枚举,比如“我喜欢猫、狗和海豚”:

Template.myTemplate.helpers({
likedAnimals: function(){return ['dogs','cats','dolphins'];}
});

<template name='myTemplate'>
I like
{{#each likedAnimals}}
{{#if !$first && !$last}}, {{/if}}
{{#if $last}} and {{/if}}
{{this}}
{{/each}}
</template>

有没有办法在 Meteor 中检查这种情况?

最佳答案

如果你们中的任何人想知道如何对集合游标做同样的事情,感谢handlebar-helpers,这里有一个更简单的方法。包。

然后你可以使用:

$mapped - will map $first, $last, and $index onto your cursor or array

像这样在模板中与 $last 助手结合:

{{#each $mapped myCursor}}
{{name}}{{#unless $last}},{{/unless}}
{{/each}}

PS:这也适用于数组

关于javascript - 在 Meteor 中使用#each,检查集合中的 'last' 元素是否到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com