Skip to content

Array Functions ​

append ​

ts
arr.append(elem: any...): array

Adds one or more elements to the end of an array and returns a new array.

Arguments: ​

  • elem (any) - Any amount of elements to add to the array

Input Example: ​

textwire
{{ ["one", "two"].append("three", "four") }}

Output: ​

html
one, two, three, four

contains ​

ts
arr.contains(elem: any): boolean

Returns true if the array contains the given element, otherwise false.

Arguments: ​

  1. elem (any) - The element to search for in the array. Can be any type, including objects and arrays

Input Example: ​

textwire
{{ ["one", "two"].contains("two") }}

Output: ​

html
1

Notes ​

You can do deep comparison with objects and arrays as well

textwire
{{ obj = { name: 'Anna' }; [obj].contains(obj) }}

Keep in mind that the order of object's fields doesn't matter in the comparison, but the order of array elements does matter, because each element has a unique index. [1, 2] and [2, 1] are different arrays.

json ​

ts
arr.json(): string

Converts array to a JSON string and returns it.

Input Example: ​

textwire
{{ ["one", "two", false, 10].json() }}

Output: ​

html
["one","two",false,10]

Important Notes ​

  • Raw (non-encoded) JSON. To get non-encoded JSON, use raw embedding:
    textwire
    {!! myArr.json() !!}
    {{-- Output: ["one","two",false,10] --}}
  • Pass to JavaScript through props. Look at this Vue.js example showing how to pass JSON array from Textwire to JavaScript frameworks using props:
    textwire
    <post-likes :post="{{ likes.json() }}" />
  • Non-finite floats become null in JSON. +Inf, -Inf, and NaN are serialized as null.

join ​

ts
arr.join(separator?: string = ","): string

Joins the elements of an array into a string and returns it. It takes an optional argument separator which is used to join the elements. If no separator is provided, it defaults to a comma.

Arguments: ​

  1. separator (string) (optional) - What separator to use to join the elements. Default is "," (comma)

Input Example: ​

textwire
{{ ["one", "two"].join(" ") }}

Output: ​

html
one two

len ​

ts
arr.len(): integer

Returns the length of an array.

Input Example: ​

textwire
{{ [1, 2, 3].len() }}

Output: ​

html
3

prepend ​

ts
arr.prepend(elem: any...): array

Adds one or more elements to the beginning of an array and returns a new array.

Arguments: ​

  • elem (any) - Any amount of elements to add to the array

Input Example: ​

textwire
{{ ["three", "four"].prepend("one", "two") }}

Output: ​

html
one, two, three, four

rand ​

ts
arr.rand(): any

Returns a random element from the array. The return type depends on the type of elements in the array.

Input Example: ​

textwire
{{ [1, 2, 3].rand() }}

Output: ​

html
2

reverse ​

ts
arr.reverse(): array

Reverses the elements of an array and returns a new array.

Input Example: ​

textwire
{{ [1, 2, 3].reverse() }}

Output: ​

html
3, 2, 1

shuffle ​

ts
arr.shuffle(): array

Shuffles the elements of an array and returns a new array.

Input Example: ​

textwire
{{ [1, 2, 3, 5].shuffle() }}

Output: ​

html
<!-- The order of the elements will be random -->
2, 1, 3, 5

slice ​

ts
arr.slice(start: integer, end?: integer): array
  • Returns a portion of an array, from start up to but not including end
  • If end is not provided, it slices to the end of the array
  • If start is greater than end, it returns an empty array

Arguments: ​

  1. start (integer) - The index at which to begin the slice
  2. end (integer) (optional) - The index at which to end the slice

Input Example: ​

textwire
{{ [1, 2, 3, 4, 5].slice(1, 3) }}

Output: ​

html
2, 3

No Negative Arguments

start and end arguments cannot be negative. If you provide a negative value for start, it will be treated as 0. If you provide a negative value for end or the value exceeds the length of the array, it will default to the last index of the array