each

The each function is used to iterate through each item of an array. Comparable to a foreach loop.

The current item is accessible via the $this variable.

The response values from an each call is ignored. each is useful when you want to modify values in-place.

Most commonly used with:

Examples

Modifying data in-place

$ echo '[1,2,3]' | dasel -i json 'each($this = $this+1)' 
[
    2,
    3,
    4
]

Extracting nested properties

[
    {"x": "foo"},
    {"x": "bar"},
    {"x": "baz"}
].map(x)
// ["foo", "bar", "baz"]

Fizzbuzz

Given numbers.json

{
    "numbers": [
        1, 2, 3, 4, 5,
        6, 7, 8, 9, 10,
        11, 12, 13, 14, 15
    ]
}
$ cat numbers.json | dasel -i json 'numbers.map(
    if ($this % 3 == 0 && $this % 5 == 0) {
        "fizzbuzz"
    } elseif ($this % 5 == 0) {
        "buzz"
    } elseif ($this % 3 == 0) {
        "fizz"
    } else {
        $this
    }
)'
[
    1,
    2,
    "fizz",
    4,
    "buzz",
    "fizz",
    7,
    8,
    "fizz",
    "buzz",
    11,
    "fizz",
    13,
    14,
    "fizzbuzz"
]

Last updated