Skip to content

add content on conditionally returning values from map() #41

Description

@rickspencer3

depending on your situation, you can use if, then, else, or dictionaries for this. here is an example with dictionaries:
import "array"
import "dict"

d = ["field1": 10.0, "field2":20.0]

array.from(rows: [{_time: 2020-01-01T00:00:00Z, _field: "field1", _value: 1.0},
{_time: 2020-01-02T00:00:00Z, _field: "field2",_value: 1.0}])
|> map(fn: (r) => ({r with _value: r._value * dict.get(dict: d, key: r._field, default: 0.0)}))

here is the same thing but using conditional logic:
import "array"

array.from(rows: [{_time: 2020-01-01T00:00:00Z, _field: "field1", _value: 1.0},
{_time: 2020-01-02T00:00:00Z, _field: "field2",_value: 1.0}])
|> map(fn: (r) => ({r with _value: if r._field == "field1"
then r._value* 10.0
else
if r._field == "field2"
then r._value * 20.0
else 0.0}))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions