@tremendous/help
    Preparing search index...

    Function set

    Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.

    The type of the object.

    The object to modify.

    The path of the property to set.

    The value to set.

    • The modified object.
    // Set a value in a nested object
    const obj = { a: { b: { c: 3 } } };
    set(obj, 'a.b.c', 4);
    console.log(obj.a.b.c); // 4
    // Set a value in an array
    const arr = [1, 2, 3];
    set(arr, 1, 4);
    console.log(arr[1]); // 4
    // Create non-existent path and set value
    const obj = {};
    set(obj, 'a.b.c', 4);
    console.log(obj); // { a: { b: { c: 4 } } }
    • Sets the value at the specified path of the given object. If any part of the path does not exist, it will be created.

      Type Parameters

      • T

        The type of the object.

      Parameters

      • obj: object

        The object to modify.

      • path: PropertyKey | readonly PropertyKey[]

        The path of the property to set.

      • value: unknown

        The value to set.

      Returns T

      • The modified object.
      // Set a value in a nested object
      const obj = { a: { b: { c: 3 } } };
      set(obj, 'a.b.c', 4);
      console.log(obj.a.b.c); // 4
      // Set a value in an array
      const arr = [1, 2, 3];
      set(arr, 1, 4);
      console.log(arr[1]); // 4
      // Create non-existent path and set value
      const obj = {};
      set(obj, 'a.b.c', 4);
      console.log(obj); // { a: { b: { c: 4 } } }