Documentation

array.map() function

The array.map() function is experimental and subject to change at any time. By using this function, you accept the risks of experimental functions.

array.map() iterates over an array, applies a function to each element to produce a new element, and then returns a new array.

import "experimental/array"

array.map(
    arr: [1, 2, 3, 4],
    fn: (x) => x * 2,
)

// Returns [2, 4, 6, 8]

Parameters

arr

Array to operate on. Default is the piped-forward array (<-).

fn

Function to apply to elements. The element is represented by x in the function.

Examples

Convert an array of integers to an array of records

import "experimental/array"

a = [1, 2, 3, 4, 5]
b = a |> array.map(fn: (x) => ({_value: x}))
// b returns [{_value: 1}, {_value: 2}, {_value: 3}, {_value: 4}, {_value: 5}]

// Output the array of records as a table
> array.from(rows: b)

Iterate over and modify an array of records

a = 
    [
        {a: 1, b: 2, c: 3},
        {a: 4, b: 5, c: 6},
        {a: 7, b: 8, c: 9},
    ]

b = a |> array.map(fn: (x) => ({x with a: x.a * x.a, d: x.b + x.c}))
// b returns:
// [
//     {a: 1, b: 2, c: 3, d: 5},
//     {a: 16, b: 5, c: 6, d: 11},
//     {a: 49, b: 8, c: 9, d: 17}
// ]

// Output the modified array of records as a table
array.from(rows: b)

Was this page helpful?

Thank you for your feedback!


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.