Back to Home
$jsonArrayUnshift
$jsonArrayUnshift adds a value to the front of a JSON
array. All existing elements shift up by one position.
Syntax
$jsonArrayUnshift[Key;...;Value]
-
Key: one or more keys forming the path to the array. Value: the value to add at the front.
What happens
$jsonParseloads the JSON object.-
$jsonArrayUnshiftinserts the value at index0of the array at the given key path. - All existing elements move up by one index.
-
Nothing is returned. Use
$jsonJoinArrayor$jsonto see the result.
Example 1: Add to the front of an array
$nomention
$jsonParse[{"music":["Ping! 2","Tokyo"\]}]
$jsonArrayUnshift[music;Paranoid]
$jsonJoinArray[music;, ]
What happens:
$jsonParseloads the object.-
$jsonArrayUnshift[music;Paranoid]insertsParanoidat the front. $jsonJoinArrayshows the result.
Output:
Paranoid, Ping! 2, Tokyo
Example 2: Prepend multiple values
$nomention
$jsonParse[{"queue":["third"\]}]
$jsonArrayUnshift[queue;second]
$jsonArrayUnshift[queue;first]
$jsonJoinArray[queue;, ]
What happens:
-
$jsonParseloads the object with a single-item array. -
$jsonArrayUnshift[queue;second]insertssecondat position0. -
$jsonArrayUnshift[queue;first]insertsfirstat position0. $jsonJoinArrayshows the result.
Output:
first, second, third
Common uses
- Priority queues: insert high-priority items at the front
- Reverse chronological logs: add new entries to the top of a list
- Prepending values: add something before all existing elements
See also
- $jsonArrayAppend: to add to the end instead
- $jsonArrayShift: to remove from the front
- $jsonJoinArray: to display the array as a string