influxdata / influxdata/awesome

add content on conditionally returning values from map()

Open
#41 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
SCSS
Stars
14
Forks
7
PR merge metrics
No merged PRs in 30d

Description

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}))

Contributor guide

No contributing guide indexed for this repository

Research direction

No target file, test, or entry point is named. Inspect the repository for existing documentation or example entries, then add the provided conditional map() and dictionary examples; done means the new content is included in the appropriate documentation location.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.