-
Notifications
You must be signed in to change notification settings - Fork 1
setPath$
Subhajit Sahu edited this page Dec 5, 2022
·
10 revisions
Set value at path in a nested map.
Similar: hasPath, getPath, setPath$, removePath$.
Similar: get, set, remove.
function setPath$(x, p, v)
// x: a nested map (updated)
// p: path
// v: value
const map = require('extra-map');
var x = new Map([['a', 2], ['b', 4], ['c', 6]]);
var y = new Map([['x', x], ['e', 10], ['f', 12]]);
map.setPath$(y, ['e'], 100);
// → Map(3) {
// → 'x' => Map(3) { 'a' => 2, 'b' => 4, 'c' => 6 },
// → 'e' => 100,
// → 'f' => 12
// → }
y;
// → Map(3) {
// → 'x' => Map(3) { 'a' => 2, 'b' => 4, 'c' => 6 },
// → 'e' => 100,
// → 'f' => 12
// → }
map.setPath$(y, ['x', 'b'], 40);
// → Map(3) {
// → 'x' => Map(3) { 'a' => 2, 'b' => 40, 'c' => 6 },
// → 'e' => 100,
// → 'f' => 12
// → }
map.setPath$(y, ['x', 'b', 'c'], 60);
// → Map(3) { (no effect)
// → 'x' => Map(3) { 'a' => 2, 'b' => 40, 'c' => 6 },
// → 'e' => 100,
// → 'f' => 12
// → }