Function setProp

  • Sets a value at a nested property path, creating intermediate objects as needed

    Type Parameters

    • T extends NestedObject
    • P extends string

    Parameters

    • obj: T

      The object to set the property in

    • path: P

      The dot-notation path to the property (e.g., 'user.name')

    • value: any

      The value to set

    Returns T

    The modified object

    Throws

    Error if input validation fails

    Example

    const obj = { user: { name: 'Flavio Ever' } };
    setProp(obj, 'user.name', 'Flavio'); // Updates name to 'Flavio'
    setProp(obj, 'user.profile.social.twitter', '@flavioever'); // Creates nested objects automatically
    setProp(obj, 'user.skills.0', 'JavaScript'); // Works with arrays