Files
30-seconds-of-code/snippets/common-keys.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

596 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
Common keys object chalarangelo symmetry-cloudy-mountain 2022-04-23T05:00:00-04:00

Finds the common keys between two objects.

  • Use Object.keys() to get the keys of the first object.
  • Use Object.prototype.hasOwnProperty() to check if the second object has a key that's in the first object.
  • Use Array.prototype.filter() to filter out keys that aren't in both objects.
const commonKeys = (obj1, obj2) =>
  Object.keys(obj1).filter(key => obj2.hasOwnProperty(key));
commonKeys({ a: 1, b: 2 }, { a: 2, c: 1 }); // ['a']