Function set

  • Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties. Use setWith to customize path creation.

    Note: This method mutates object.

    Type Parameters

    • T extends object

    Parameters

    • object: T

      The object to modify.

    • path: PropertyPath

      The path of the property to set.

    • value: any

      The value to set.

    Returns T

    Returns object.

    3.7.0

    has, hasIn, get, unset

    const object = { 'a': [{ 'b': { 'c': 3 } }] }

    set(object, 'a[0].b.c', 4)
    console.log(object.a[0].b.c)
    // => 4

    set(object, ['x', '0', 'y', 'z'], 5)
    console.log(object.x[0].y.z)
    // => 5