Back to Home
$jsonArrayPop
$jsonArrayPop removes the last element from a JSON array
and returns it.
Syntax
$jsonArrayPop[Key;...]
-
Key: one or more keys forming the path to the array.
What happens
$jsonParseloads the JSON object.-
$jsonArrayPopremoves the last element from the array and returns its value. - The array is one element shorter after this call.
Example 1: Pop the last element
$nomention
$jsonParse[{"music":["Paranoid","Ping! 2","Tokyo"\]}]
Removed: $jsonArrayPop[music]
Remaining: $jsonJoinArray[music;, ]
What happens:
$jsonParseloads the object.-
$jsonArrayPop[music]removesTokyofrom the end and returns it. $jsonJoinArrayshows what is left.
Output:
Removed: Tokyo
Remaining: Paranoid, Ping! 2
Example 2: Pop until empty
$nomention
$jsonParse[{"stack":["first","second","third"\]}]
> $jsonArrayPop[stack]
> $jsonArrayPop[stack]
Remaining: $jsonJoinArray[stack;, ]
What happens:
$jsonParseloads the object.-
The first
$jsonArrayPopremoves and returnsthird. - The second removes and returns
second. $jsonJoinArrayshows what is left.
Output:
> third
> second
Remaining: first
Common uses
- Queue systems: process and remove the most recently added item
- Undo functionality: pop the last item from a history list
- Reducing a list: remove trailing entries one at a time
See also
- $jsonArrayShift: to remove the first element instead
- $jsonArrayAppend: to add elements to the end
- $jsonJoinArray: to display the remaining array after popping