docs-v2/content/flux/v0.x/stdlib/dict/insert.md

1.7 KiB

title description aliases menu weight introduced
dict.insert() function The `dict.insert()` function inserts a key value pair into a dictionary and returns a new, updated dictionary. If the key already exists in the dictionary, the function overwrites the existing value.
/influxdb/v2.0/reference/flux/stdlib/dict/insert/
/influxdb/cloud/reference/flux/stdlib/dict/insert/
flux_0_x_ref
name parent
dict.insert dict
301 0.97.0

The dict.insert() function inserts a key value pair into a dictionary and returns a new, updated dictionary. If the key already exists in the dictionary, the function overwrites the existing value.

import "dict"

dict.insert(
    dict: [1: "foo", 2: "bar"],
    key: 3,
    value: "baz",
)

Parameters

{{< req "All paremeters are required" >}}

dict

Dictionary to update.

key

Key to insert into the dictionary. Must be the same type as existing keys in the dictionary.

default

Value to insert into the dictionary. Must be the same type as existing values in the dictionary.

Examples

Insert a new key-value pair into a dictionary
import "dict"

d = [1: "foo", 2: "bar"]

dNew = dict.insert(dict: d, key: 3, value: "baz")

// Verify the new key-value pair was inserted
dict.get(dict: dNew, key: 3, default: "")

// Returns baz
Overwrite an existing key-value pair in a dictionary
import "dict"

d = [1: "foo", 2: "bar"]

dNew = dict.insert(dict: d, key: 2, value: "baz")

// Verify the new key-value pair was overwritten
dict.get(dict: dNew, key: 2, default: "")

// Returns baz