Skip to content

@technobuddha > library > Object > Comparison

Function: shallowEquals()

ts
function shallowEquals(
   objA: 
  | undefined
  | null
  | Record<string, unknown>, 
   objB: 
  | undefined
  | null
  | Record<string, unknown>, 
   exclude: string[]): boolean;

Defined in: shallow-equals.ts:21

Compare two object for equality. Testing goes one level deep.

Parameters

ParameterTypeDefault valueDescription
objA| undefined | null | Record<string, unknown>undefinedFirst object to compare
objB| undefined | null | Record<string, unknown>undefinedSecond object to compare
excludestring[][]Array of key names to exclude from the comparison

Returns

boolean

true if the two objects have the same members

Example

typescript
const a = \{ x: 1, y: 2 \};
const b = \{ x: 1, y: 2 \};
shallowEquals(a, b); // true
shallowEquals(a, \{ x: 1 \}); // false
shallowEquals(a, \{ x: 1, y: 3 \}); // false
shallowEquals(a, b, ['y']); // true