ModelWorks logoModelWorks
JSON TransformArray

reverse

Reverse the order of array elements

reverse

Reverses the order of all elements in an array.

Options

This operation takes no options; pass an empty object or omit options entirely.

Example

Input

JSON
[1, 2, 3, 4]

Operation

JSON
{ "type": "reverse" }

Output

JSON
[4, 3, 2, 1]

In a Circuit Step

JSON
{
  "name": "reverse_history",
  "description": "Reverse the order of messages so the newest is first",
  "function": "circuit.core.transform.json",
  "input": {
    "source": "${{ messages }}",
    "operations": [
      { "type": "reverse" }
    ]
  },
  "outputs": [
    {
      "name": "reversed_messages",
      "description": "Messages in reverse chronological order",
      "value": "${{ reverse_history.output.transformed_result }}"
    }
  ]
}

Notes & gotchas

  • Errors with reverse: expected array if the input is not an array.
  • Returns a new array; the original is not mutated.
  • Pairs well with sort when you want descending order on a non-string key without specifying "order": "desc".
On this page

On this page