Back to Home
$jsonArrayShift
$jsonArrayShift removes the first element from a JSON array
and returns it.
Syntax
$jsonArrayShift[Key;...]
-
Key: one or more keys forming the path to the array.
What happens
$jsonParseloads the JSON object.-
$jsonArrayShiftremoves the first element from the array and returns its value. - All remaining elements shift down by one position.
Example 1: Shift the first element
$nomention
$jsonParse[{"music":["Paranoid","Ping! 2","Tokyo"\]}]
Removed: $jsonArrayShift[music]
Remaining: $jsonJoinArray[music;, ]
What happens:
$jsonParseloads the object.-
$jsonArrayShift[music]removesParanoidfrom the front and returns it. $jsonJoinArrayshows what is left.
Output:
Removed: Paranoid
Remaining: Ping! 2, Tokyo
Example 2: Shift until empty
$nomention
$jsonParse[{"queue":["first","second","third"\]}]
> $jsonArrayShift[queue]
> $jsonArrayShift[queue]
Remaining: $jsonJoinArray[queue;, ]
What happens:
$jsonParseloads the object.-
The first
$jsonArrayShiftremoves and returnsfirst. - The second removes and returns
second. $jsonJoinArrayshows what is left.
Output:
> first
> second
Remaining: third
Common uses
- Queue systems: process items in order from the front (FIFO)
- Consuming lists: work through a list from the beginning
- Removing old entries: drop the oldest item from a history array
See also
- $jsonArrayPop: to remove the last element instead
- $jsonArrayUnshift: to add an element to the front
- $jsonJoinArray: to display the remaining array after shifting