JS check if two object equal code example
Example 1: javascript check if objects are equal
const isEqual = (...objects) => objects.every(obj => JSON.stringify(obj) === JSON.stringify(objects[0]));
isEqual({ foo: 'bar' }, { foo: 'bar' });
isEqual({ foo: 'bar' }, { bar: 'foo' });
Example 2: check if objects are equal javascript
function isEquivalent(a, b) {
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}
console.log(isEquivalent(bobaFett, jangoFett));