setDeep
A convenience utility for setting the value of a key deep within an object.
Useful when implementing field transforms.
import { setDeep } from "@measured/puck";
const newData = setDeep(
{
object: {
array: [{ key: "Hello, world" }],
},
},
"object.array[0].key",
"Goodbye, world"
);
console.log(newData);
// {
// object: {
// array: [{ key: "Goodbye, world" }],
// },
// }
Args
Param | Example | Type |
---|---|---|
input | {} | Object | Array |
path | "object.array[0].key" | String |
value | "Hello, world" | Any |
input
The input object or array that contains the key to be updated.
path
A dot-notated path that describes a key deep within an object or array.
Uses dot-notation and square brackets:
.
deliminates sub-props in an object field (i.e.a.b
representsb
wherea
is{b: "Value"}
)[*]
which donate an index in an array field where*
is an integer (i.e.a[0].b
representsb
in the first item in arraya
with the value[{b: "Value"}]
)
Commonly provided by the propPath parameter for field transforms.
value
The value to set the key to.