Skip to main content
Version: Next

compareObjects

Using

Function that compares two objects and returns either the differences between them or their common properties, based on a specified return type parameter.

Demo - use JSON
compareObjects(
,
,
)
{"a":12,"c":50,"d":1}

Arguments

ArgumentTypeDescriptionExample
obj1objectFirst object for compare.{ a: "value", b: 100, c: 10 }
obj2objectSecond object for compare.{ a: 12, c: 50, d: 1 }
returnTypestringreturnType can be of two values:
- "diff" - the function should return the differences between the two objects.
- "common" - the function should return the common properties of the two objects.
{ a: 12, c: 50, d: 1 }

Returns

object — Compared objects.

Examples

const obj1 = { a: 1, b: 2, c: { d: 3 } };
const obj2 = { a: 1, b: 3, c: { d: 4 } };
compareObjects(obj1, obj2);
// { b: 3, c: { d: 4 } }

const obj1 = { a: 1, b: 2, c: { d: 3 } };
const obj2 = { a: 1, b: 3, c: { d: 3 } };
compareObjects(obj1, obj2, 'common');
// { a: 1, c: { d: 3 } }

const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1 };
compareObjects(obj1, obj2);
// { b: undefined }

Resources

📦 Since:1.2.0
📦 Last updated:1.2.0
📦 Available in:1.3.0