Skip to content

Instantly share code, notes, and snippets.

@Paratron
Created April 4, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paratron/b504210b5ace4a56830dc223bebfd003 to your computer and use it in GitHub Desktop.
Save Paratron/b504210b5ace4a56830dc223bebfd003 to your computer and use it in GitHub Desktop.
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