Common keys

Angelos Chalaris · JavaScript, Object · Apr 23, 2022

Finds the common keys between two objects.

const commonKeys = (obj1, obj2) =>
  Object.keys(obj1).filter(key => obj2.hasOwnProperty(key));
commonKeys({ a: 1, b: 2 }, { a: 2, c: 1 }); // ['a']

Recommended snippets

  • Deep merge objects

    JavaScript, Object

    Deeply merges two objects, using a function to handle keys present in both.

  • Find first matching key

    JavaScript, Object

    Finds the first key that satisfies the provided testing function. Otherwise undefined is returned.

  • Find last matching key

    JavaScript, Object

    Finds the last key that satisfies the provided testing function. Otherwise undefined is returned.