QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Compare Two Objects Shallowly

Compares two objects shallowly and returns whether they are equal.

  • object
  • compare
  • shallow

Install

npx shadcn@latest add https://quicksnip.dev/r/compare-two-objects-shallowly.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function shallowEqual(obj1, obj2) {
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);
  if (keys1.length !== keys2.length) return false;
  return keys1.every(key => obj1[key] === obj2[key]);
}

Usage

const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };
const obj3 = { a: 1, b: 3 };
shallowEqual(obj1, obj2); // Returns: true
shallowEqual(obj1, obj3); // Returns: false