Generate experiences with Puck AI
Read docs
API ReferenceFunctionssetDeep

setDeep

A convenience utility for setting the value of a key deep within an object.

Useful when implementing field transforms.

This function only duplicates the objects and arrays along the path to the key being set, leaving the rest of the input object or array unchanged.

import { setDeep } from "@puckeditor/core";
 
const newData = setDeep(
  {
    object: {
      array: [{ key: "Hello, world" }],
    },
  },
  "object.array[0].key",
  "Goodbye, world"
);
 
console.log(newData);
// {
//   object: {
//     array: [{ key: "Goodbye, world" }],
//   },
// }

Args

ParamExampleType
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 represents b where a is {b: "Value"})
  • [*] which donate an index in an array field where * is an integer (i.e. a[0].b represents b in the first item in array a with the value [{b: "Value"}])

Commonly provided by the propPath parameter for field transforms.

value

The value to set the key to.