Skip to content

Instantly share code, notes, and snippets.

@alalfakawma
Created February 5, 2022 19:21
Show Gist options
  • Save alalfakawma/7313eca08ab7a2508ece5e9254521c0a to your computer and use it in GitHub Desktop.
Save alalfakawma/7313eca08ab7a2508ece5e9254521c0a to your computer and use it in GitHub Desktop.
/*
* Check keys of objects
* return false if atleast 1 key does not match
* return true if all keys are the same
*
* @param key Array<string> | string
* @param obj1 { [key: string]: string }
* @param obj2 { [key: string]: string }
* @return boolean
*/
function objectCompare<T>(key: Array<string> | string, obj1: { [key: string]: T }, obj2: { [key: string]: T }): boolean {
if (Array.isArray(key)) {
for (let i = 0; i < key.length; i++) {
if (obj1[key[i]] !== obj2[key[i]]) return false;
}
} else {
if (obj1[key] !== obj2[key]) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment