Sidechair

map.copy()

Browse the reference

Returns a new map with the same pairs, so changes to one map no longer affect the other.

Try it in the editor

The copy is shallow: if the values are objects, both maps still point at the same objects. For numbers, strings and colors the two maps are fully independent.

Take a copy before experimenting with a map that other code reads.

Syntax

Pine Script
map.copy(id: any map type) -> map<keyType, valueType>
map.copy() -> map<keyType, valueType>

Arguments

NameTypeDefaultDescription
idany map typerequiredThe map to duplicate. The copy holds the same pairs and is a separate map.

Returns

map<keyType, valueType>: A new map with the same key and value types and pairs.

Example

Pine ScriptSnapshot and compareOpen in editor
//@version=6
indicator("map.copy: snapshot", overlay = false)
var live = map.new<string, float>()
map.put(live, "close", close)
snap = map.copy(live)
map.put(live, "close", close * 2)
plot(map.get(snap, "close"), "Snapshot")
plot(map.get(live, "close"), "Live")

See also

Runs on Sidechair