can-key/set/set
Set properties on deep/nested objects of different types: Object, Map, can-reflect types, etc.
set(object, path, value)
Parameters
- object
{Object}
:The object to use as the root for property-based navigation.
- path
{String}
:A String of dot-separated keys, representing a path of properties.
- value
{*}
:The new value to be set at the property path.
Returns
{*}
:
The object passed to set (for chaining calls).
A path is a dot-delimited sequence of one or more property names, such that "foo.bar" means "the property 'bar' of the object at the property 'foo' of the root."
import set from "can-key/set/set";
const object = {a: {b: {c: "foo"}}};
set(object, "a.b.c", "bar");
// Now object.a.b.c === "bar"
var map = new Map();
map.set("first", {second: "third"});
set(map, "first.second", "3rd");
// Now map.first.second === "3rd"
Note: an error will be thrown if one of the objects in the key path does not exist.