common-utility-functions
    Preparing search index...

    Function propertiesEqual

    • Compares two objects for equality based only on specified properties. Extracts the specified properties from both objects and performs a deep equality comparison on the resulting subsets. This is useful when you want to compare objects while ignoring certain properties.

      Type Parameters

      • K extends string
      • T extends Record<K, undefined | null | string | number | T>

      Parameters

      • obj1: T

        The first object to compare

      • obj2: T

        The second object to compare

      • properties: string[]

        An array of property names to include in the comparison

      Returns boolean

      True if the specified properties are deeply equal between the objects, false otherwise

      const user1 = { id: 1, name: 'John', email: 'john@example.com', lastLogin: '2023-01-01' };
      const user2 = { id: 1, name: 'John', email: 'john@example.com', lastLogin: '2023-01-02' };

      // Compare only name and email (ignoring id and lastLogin)
      propertiesEqual(user1, user2, ['name', 'email']); // true

      // Compare all properties
      propertiesEqual(user1, user2, ['id', 'name', 'email', 'lastLogin']); // false

      // Empty properties array always returns true
      propertiesEqual(user1, user2, []); // true

      // Works with nested objects
      const obj1 = { a: 1, b: { c: 2, d: 3 }, e: 4 };
      const obj2 = { a: 1, b: { c: 2, d: 4 }, e: 5 };
      propertiesEqual(obj1, obj2, ['a']); // true
      propertiesEqual(obj1, obj2, ['b']); // false

      1.0.0