Skip to content

setPath$

Subhajit Sahu edited this page Dec 8, 2022 · 3 revisions

Set value at path in a nested object.

Similar: set, setPath$.
Similar: hasPath, getPath, removePath$.
Similar: get, set, swap, remove.


function setPath$(x, p, v)
// x: a nested object (updated)
// p: path
// v: value
const object = require('extra-object');

var x = {a: {b: 2, c: 3}, d: 4};
object.setPath$(x, ['d'], 40);
// → { a: { b: 2, c: 3 }, d: 40 }

x;
// → { a: { b: 2, c: 3 }, d: 40 }

object.setPath$(x, ['a', 'b'], 20);
// → { a: { b: 20, c: 3 }, d: 40 }

object.setPath$(x, ['a', 'b', 'c'], 30);
// → { a: { b: 20, c: 3 }, d: 40 } (no effect)


References

Clone this wiki locally