Array
Visit the: code | unit tests | issues)
- after (code | tests)
- arrayify (code | tests)
- before (code | tests)
- eachIndex (code | tests)
- filter (code | tests)
- first (code | tests)
- forEach (code | tests)
- inArray (code | tests)
- isArray (code | tests)
- itemAt (code | tests)
- join (code | tests)
- equalsLength (code | tests)
- last (code | tests)
- length (code | tests)
- lengthEqual (code | tests)
- map (code | tests)
- pluck (code | tests)
- reverse (code | tests)
- some (code | tests)
- sort (code | tests)
- sortBy (code | tests)
- withAfter (code | tests)
- withBefore (code | tests)
- withFirst (code | tests)
- withGroup (code | tests)
- withLast (code | tests)
- withSort (code | tests)
- unique (code | tests)
{after}
Returns all of the items in an array after the specified index. Opposite of before.
Params
array
Array: Collectionn
Number: Starting index (number of items to exclude)returns
Array: Array exludingn
items.
Example
{{after array 1}}
{arrayify}
Cast the given value
to an array.
Params
value
anyreturns
Array
Example
{{arrayify "foo"}}
{before}
Return all of the items in the collection before the specified count. Opposite of after.
Params
array
Arrayn
Numberreturns
Array: Array excluding items after the given number.
Example
{{before array 2}}
{eachIndex}
Params
array
Arrayoptions
Objectreturns
String
Example
{{#eachIndex array}}
{{item}} is {{index}}
{{/eachIndex}}
{filter}
Block helper that filters the given array and renders the block for values that evaluate to true
, otherwise the inverse block is returned.
Params
array
Arrayvalue
anyoptions
Objectreturns
String
Example
{{#filter array "foo"}}AAA{{else}}BBB{{/filter}}
{first}
Returns the first item, or first n
items of an array.
Params
array
Arrayn
Number: Number of items to return, starting at0
.returns
Array
Example
{{first "['a', 'b', 'c', 'd', 'e']" 2}}
{forEach}
Iterates over each item in an array and exposes the current item in the array as context to the inner block. In addition to the current array item, the helper exposes the following variables to the inner block:
index
total
isFirst
isLast
Also,@index
is exposed as a private variable, and additional private variables may be defined as hash arguments.
Params
array
Arrayreturns
String
Example
{{#forEach accounts}}
<a href="mailto:{{ email }}" title="Send an email to {{ name }}">
{{ name }}
a>{{#unless isLast}}, {{/unless}}
{{/forEach}}
{inArray}
Block helper that renders the block if an array has the given value
. Optionally specify an inverse block to render when the array does not have the given value.
Params
array
Arrayvalue
anyoptions
Objectreturns
String
Example
{{#inArray array "d"}}
foo
{{else}}
bar
{{/inArray}}
{isArray}
Returns true if value
is an es5 array.
Params
value
any: The value to test.returns
Boolean
Example
{{isArray "abc"}}
{{isArray array}}
{itemAt}
Returns the item from array
at index idx
.
Params
array
Arrayidx
Numberreturns
anyvalue
Example
{{itemAt array 1}}
{join}
Join all elements of array into a string, optionally using a given separator.
Params
array
Arrayseparator
String: The separator to use. Defaults to,
.returns
String
Example
{{join array}}
{{join array '-'}}
{equalsLength}
Returns true if the the length of the given value
is equal
to the given length
. Can be used as a block or inline helper.
Params
value
Array|Stringlength
Numberoptions
Objectreturns
String
{last}
Returns the last item, or last n
items of an array or string. Opposite of first.
Params
value
Array|String: Array or string.n
Number: Number of items to return from the end of the array.returns
Array
Example
{{last value}}
{{last value 2}}
{{last value 3}}
{length}
Returns the length of the given string or array.
Params
value
Array|Object|Stringreturns
Number: The length of the value.
Example
{{length '["a", "b", "c"]'}}
{{length myArray}}
{{length myObject}}
{lengthEqual}
Alias for equalsLength
{map}
Returns a new array, created by calling function
on each element of the given array
. For example,
Params
array
Arrayfn
Functionreturns
String
Example
{{map array double}}
{pluck}
Map over the given object or array or objects and create an array of values from the given prop
. Dot-notation may be used (as a string) to get nested properties.
Params
collection
Array|Objectprop
Functionreturns
String
Example
// {{pluck items "data.title"}}
{reverse}
Reverse the elements in an array, or the characters in a string.
Params
value
Array|Stringreturns
Array|String: Returns the reversed string or array.
Example
{{reverse value}}
{{reverse value}}
{some}
Block helper that returns the block if the callback returns true for some value in the given array.
Params
array
Arrayiter
Function: Iteratee- Options: Handlebars provided options object
returns
String
Example
{{#some array isString}}
Render me if the array has a string.
{{else}}
Render me if it doesn't.
{{/some}}
{sort}
Sort the given array
. If an array of objects is passed, you may optionally pass a key
to sort on as the second argument. You may alternatively pass a sorting function as the second argument.
Params
array
Array: the array to sort.key
String|Function: The object key to sort by, or sorting function.
Example
{{sort array}}
{sortBy}
Sort an array
. If an array of objects is passed, you may optionally pass a key
to sort on as the second argument. You may alternatively pass a sorting function as the second argument.
Params
array
Array: the array to sort.props
String|Function: One or more properties to sort by, or sorting functions to use.
Example
{{sortBy array "a"}}
{withAfter}
Use the items in the array after the specified index as context inside a block. Opposite of withBefore.
Params
array
Arrayidx
Numberoptions
Objectreturns
Array
Example
{{#withAfter array 3}}
{{this}}
{{/withAfter}}
{withBefore}
Use the items in the array before the specified index as context inside a block. Opposite of withAfter.
Params
array
Arrayidx
Numberoptions
Objectreturns
Array
Example
{{#withBefore array 3}}
{{this}}
{{/withBefore}}
{withFirst}
Use the first item in a collection inside a handlebars block expression. Opposite of withLast.
Params
array
Arrayidx
Numberoptions
Objectreturns
String
Example
{{#withFirst array}}
{{this}}
{{/withFirst}}
{withGroup}
Block helper that groups array elements by given group size
.
Params
array
Array: The array to iterate oversize
Number: The desired length of each array "group"options
Object: Handlebars optionsreturns
String
Example
{{#withGroup array 4}}
{{#each this}}
{{.}}
{{each}}
<br>
{{/withGroup}}
{withLast}
Use the last item or n
items in an array as context inside a block. Opposite of withFirst.
Params
array
Arrayidx
Number: The starting index.options
Objectreturns
String
Example
{{#withLast array}}
{{this}}
{{/withLast}}
{withSort}
Block helper that sorts a collection and exposes the sorted collection as context inside the block.
Params
array
Arrayprop
Stringoptions
Object: Specifyreverse="true"
to reverse the array.returns
String
Example
{{#withSort array}}{{this}}{{/withSort}}
{unique}
Block helper that return an array with all duplicate values removed. Best used along with a each helper.
Params
array
Arrayoptions
Objectreturns
Array
Example
{{#each (unique array)}}{{.}}{{/each}}