Dasel
v3
v3
  • Getting started
    • Introduction
    • Installation
    • Concepts
      • Expressions
      • Branches
  • Syntax
    • Whitespace
    • Types/Literals
    • String concatenation
    • Arrays/slices
    • Objects/maps
    • Conditionals
    • Spread
    • Coalesce
  • Input/Output
    • Read/Write formats
    • Stdin
    • Stdout
    • Variables
    • Read/Writer flags
  • Functions
    • add
    • min
    • max
    • reverse
    • toString
    • toInt
    • toFloat
    • typeOf
    • map
    • filter
    • sortBy
    • parse
    • base64e
    • base64d
Powered by GitBook
On this page
  • Examples
  • Simple addition
  • Extracting nested properties
  • Fizzbuzz
  1. Functions

map

PrevioustypeOfNextfilter

Last updated 7 months ago

The map function can be used to transform the contents of an array. It's functions in a similar way to .

Examples

Simple addition

[1, 2, 3].map($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"
]

javascripts Array.prototype.map