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
Argument | Type | Description | Example |
---|---|---|---|
obj1 | object | First object for compare. | { a: "value", b: 100, c: 10 } |
obj2 | object | Second object for compare. | { a: 12, c: 50, d: 1 } |
returnType | string | returnType 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 }