MVFv3 API Documentation
    Preparing search index...
    • Deeply merges a partial object into a full object, using specified identifier keys for array merging.

      This function performs a deep merge with special handling for arrays containing objects with identifier keys. It's particularly useful for merging complex nested structures where some properties should be merged rather than replaced.

      Type Parameters

      • T extends object
      • K extends string

      Parameters

      • full: T

        The complete object to merge into.

      • partial: PartialExcept<T, K>

        The partial object containing updates. All properties are optional except those specified by idKeys.

      • OptionalidKeys: readonly K[]

        The keys used as identifiers for merging array items. Can be a single key or an array of keys. When multiple keys are provided, an object is expected to have only one of these keys.

      Returns T

      A new object that is the result of merging partial into full.

      If trying to merge an identified array into a non-array property.

      Merging behavior:

      1. Simple properties: Overwrites values in full with non-undefined values from partial.
      2. Objects: Recursively merges properties.
      3. Arrays: a. If array items have the specified idKey, it merges items with matching IDs. b. If array items don't have the idKey, it replaces the entire array.
      4. New properties: Adds properties from partial that don't exist in full.
      // Merging simple objects
      const full = { a: 1, b: 2 };
      const partial = { b: -1 };
      deepMergeOn(full, partial, 'id'); // Returns { a: 1, b: -1 }
      // Merging nested objects with arrays
      const full = {
      a: 1,
      b: 2,
      children: [
      { id: '1', x: 10 },
      { id: '2', y: 20 }
      ]
      };
      const partial = {
      b: 3,
      children: [
      { id: '2', y: 30 }
      ]
      };
      deepMergeOn(full, partial, 'id');
      // Returns:
      // {
      // a: 1,
      // b: 3,
      // children: [
      // { id: '1', x: 10 },
      // { id: '2', y: 30 }
      // ]
      // }
      // Merging arrays without identifier keys
      const full = { data: [1, 2, 3] };
      const partial = { data: [4, 5] };
      deepMergeOn(full, partial, 'id'); // Returns { data: [4, 5] }
      // Adding new properties
      const full = { a: 1 };
      const partial = { b: 2 };
      deepMergeOn(full, partial, 'id'); // Returns { a: 1, b: 2 }
      // Deeply nested merging
      const full = { a: { b: { c: 1 } } };
      const partial = { a: { b: { d: 2 } } };
      deepMergeOn(full, partial, 'id'); // Returns { a: { b: { c: 1, d: 2 } } }
      // Error case: merging identified array into non-array
      const full = { a: 1 };
      const partial = { a: [{ id: '1', value: 2 }] };
      // Throws Error: "Expected array at a, got number"
      deepMergeOn(full, partial, 'id');