ixfx
    Preparing search index...

    Function updateByPath

    • Returns a copy of target object with a specified path changed to value.

      const a = {
      message: `Hello`,
      position: { x: 10, y: 20 }
      }

      const a1 = updateByPath(a, `message`, `new message`);
      // a1 = { message: `new message`, position: { x: 10, y: 20 }}
      const a2 = updateByPath(a, `position.x`, 20);
      // a2 = { message: `hello`, position: { x: 20, y: 20 }}

      Paths can also be array indexes:

      updateByPath([`a`,`b`,`c`], 2, `d`);
      // Yields: [ `a`, `b`, `d` ]

      By default, only existing array indexes can be updated. Use the allowShapeChange parameter to allow setting arbitrary indexes.

      // Throws because array index 3 is undefined
      updateByPath([ `a`, `b`, `c` ], `3`, `d`);

      // With allowShapeChange flag
      updateByPath([ `a`, `b`, `c` ], `3`, `d`, true);
      // Returns: [ `a`, `b`, `c`, `d` ]

      Throws an error if:

      • path cannot be resolved (eg. position.z in the above example)
      • value applied to target results in the object having a different shape (eg missing a field, field changing type, or array index out of bounds). Use allowShapeChange to suppress this error.
      • Path is undefined or not a string
      • Target is undefined/null

      Type Parameters

      • VextendsRecord<string, any>

      Parameters

      • target: V

        Object to update

      • path: string

        Path to set value

      • value: any

        Value to set

      • allowShapeChange: boolean = false

        By default false, throwing an error if an update change the shape of the original object.

      Returns V