如果长度未知,则把手返回具有特定索引的数组元素

时间:2022-10-26 21:45:00

Using Handlebars and Node, I have an array with changing length and I need always to get the one before last element of it each time it changes.

使用Handlebars和Node,我有一个长度不断变化的数组,每次更改时我总是需要在它的最后一个元素之前得到一个。

So if you want to get the last element, you can do:

因此,如果您想获得最后一个元素,您可以:

{{#each array}}
  {{#if @last}}
    {{this}}
  {{/if}}
{{/each}}

and it will return you the last element. I have Math helper, that can perform basic mathematical operations. For example I can get with it the needed index number

它将返回最后一个元素。我有Math助手,可以执行基本的数学运算。例如,我可以得到所需的索引号

{{math array.length '-' 1}}

but I cant use it with another block helper, for example:

但是我不能将它与另一个块助手一起使用,例如:

{{#each array}}
  {{#if math @last '-' 1}}
    {{this}}
  {{/if}}
{{/each}}

How could this be done? Thank you.

怎么可以这样做?谢谢。

2 个解决方案

#1


1  

You can register your own helper for that:

您可以为此注册自己的助手:

Handlebars.registerHelper('isSecondLastArrayItem', function(array, index, options) {

  if((array.length - 2) === index)
    return options.fn(this);

  return;

});

And use it like this:

并像这样使用它:

{{#each array}}
  {{#isSecondLastArrayItem @root.array @index}}
    {{title}}
  {{/isSecondLastArrayItem}}
{{/each}}

Example data:

{ 
  array: [{ title: 'aa' }, { title: 'vv' }]
}

Try it out in the sandbox.

在沙盒中试一试。

EDIT: case for any index

编辑:任何索引的案例

Handlebars.registerHelper('isNthLastArrayItem', function(array, index, offset, options) {

  if(((array.length >= offset) && (array.length - offset) === index)
    return options.fn(this);

  return;

});

usage (for 2nd item from the end):

用法(从最后的第2项):

{{#each array}}
  {{#isNthLastArrayItem @root.array @index 2}}
    {{title}}
  {{/isNthLastArrayItem}}
{{/each}}

#2


2  

Firstly, you state that you cannot use your math helper within the mustaches of the #if block helper, like {{#if math @last '-' 1}}. This is not true. Handlebars supports subexpressions.

首先,你声明你不能在#if块助手的胡须中使用你的数学助手,比如{{#if math @last' - '1}}。这不是真的。把手支持子表达式。

Handlebars offers support for subexpressions, which allows you to invoke multiple helpers within a single mustache, and pass in the results of inner helper invocations as arguments to outer helpers. Subexpressions are delimited by parentheses.

Handlebars提供对子表达式的支持,允许您在一个小胡子中调用多个帮助程序,并将内部帮助程序调用的结果作为参数传递给外部帮助程序。 Subexpressions由括号分隔。

However, {{#if (math @last '-' 1)}} will not make much sense because @last is a Boolean and I would advise against the oddity of subtracting 1 from true or false.

但是,{{#if(math @last' - '1)}}没有多大意义,因为@last是一个布尔值,我建议不要从真或假中减去1的奇怪之处。

If your issue is to render a single element from your array - for example, the second to last - then I do not understand why you are #eaching through your array. All you need do is combine the built-in lookup helper with your existing math helper and your template gets a whole lot simpler:

如果你的问题是从你的数组中渲染一个元素 - 例如,倒数第二个 - 那么我不明白你为什么要在你的数组中进行#eaching。您所需要做的就是将内置的查找助手与现有的数学助手相结合,您的模板变得更加简单:

{{lookup array (math array.length '-' 2)}}

Note: Remember that JavaScript arrays are zero-indexed, so we need to subtract 2 to get the second from last element.

注意:请记住,JavaScript数组是零索引的,因此我们需要减去2以从最后一个元素中获取第二个。

I have created a simple fiddle for reference.

我创造了一个简单的小提琴供参考。

#1


1  

You can register your own helper for that:

您可以为此注册自己的助手:

Handlebars.registerHelper('isSecondLastArrayItem', function(array, index, options) {

  if((array.length - 2) === index)
    return options.fn(this);

  return;

});

And use it like this:

并像这样使用它:

{{#each array}}
  {{#isSecondLastArrayItem @root.array @index}}
    {{title}}
  {{/isSecondLastArrayItem}}
{{/each}}

Example data:

{ 
  array: [{ title: 'aa' }, { title: 'vv' }]
}

Try it out in the sandbox.

在沙盒中试一试。

EDIT: case for any index

编辑:任何索引的案例

Handlebars.registerHelper('isNthLastArrayItem', function(array, index, offset, options) {

  if(((array.length >= offset) && (array.length - offset) === index)
    return options.fn(this);

  return;

});

usage (for 2nd item from the end):

用法(从最后的第2项):

{{#each array}}
  {{#isNthLastArrayItem @root.array @index 2}}
    {{title}}
  {{/isNthLastArrayItem}}
{{/each}}

#2


2  

Firstly, you state that you cannot use your math helper within the mustaches of the #if block helper, like {{#if math @last '-' 1}}. This is not true. Handlebars supports subexpressions.

首先,你声明你不能在#if块助手的胡须中使用你的数学助手,比如{{#if math @last' - '1}}。这不是真的。把手支持子表达式。

Handlebars offers support for subexpressions, which allows you to invoke multiple helpers within a single mustache, and pass in the results of inner helper invocations as arguments to outer helpers. Subexpressions are delimited by parentheses.

Handlebars提供对子表达式的支持,允许您在一个小胡子中调用多个帮助程序,并将内部帮助程序调用的结果作为参数传递给外部帮助程序。 Subexpressions由括号分隔。

However, {{#if (math @last '-' 1)}} will not make much sense because @last is a Boolean and I would advise against the oddity of subtracting 1 from true or false.

但是,{{#if(math @last' - '1)}}没有多大意义,因为@last是一个布尔值,我建议不要从真或假中减去1的奇怪之处。

If your issue is to render a single element from your array - for example, the second to last - then I do not understand why you are #eaching through your array. All you need do is combine the built-in lookup helper with your existing math helper and your template gets a whole lot simpler:

如果你的问题是从你的数组中渲染一个元素 - 例如,倒数第二个 - 那么我不明白你为什么要在你的数组中进行#eaching。您所需要做的就是将内置的查找助手与现有的数学助手相结合,您的模板变得更加简单:

{{lookup array (math array.length '-' 2)}}

Note: Remember that JavaScript arrays are zero-indexed, so we need to subtract 2 to get the second from last element.

注意:请记住,JavaScript数组是零索引的,因此我们需要减去2以从最后一个元素中获取第二个。

I have created a simple fiddle for reference.

我创造了一个简单的小提琴供参考。