Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to test if two objects are equal
/**
* This function takes two objects and compares if they have the same
* keys and their keys have the same values assigned, so the objects are
* basically the same.
* @param {object} objA
* @param {object} objB
* @return {boolean}
*/
const objectsEqual = (objA, objB) => {
const objAKeys = Object.keys(objA);
const objBKeys = Object.keys(objB);
const valueIsEqual = key => objB.hasOwnProperty(key) && objA[key] === objB[key];
return (
objAKeys.length === objBKeys.length
&& objAKeys.every(valueIsEqual)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment