compare objects in javascript
var person1={first_name:"bob"};
var person2 = {first_name:"bob"};
//compare the two object
if(JSON.stringify(person1) === JSON.stringify(person2)){
//objects are the same
}
function isEquivalent(a, b) {
// Create arrays of property names
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
// If number of properties is different,
// objects are not equivalent
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
// If values of same property are not equal,
// objects are not equivalent
if (a[propName] !== b[propName]) {
return false;
}
}
// If we made it this far, objects
// are considered equivalent
return true;
}function shallowEqual(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (let key of keys1) {
if (object1[key] !== object2[key]) {
return false;
}
}
return true;
}
Also in JavaScript:
- get object name javascript
- what are the uses of babel js
- close exit app react native
- data in table when i click the table link in open table in another jquery
- rest api with mongodb and nodejs
- extract data from pdf nodejs
- can we import jquery library from developer tools
- curl pretty print json
- javascript flat object
- how to make a div appear when clicked on in javascript
- create nodejs new object
- reactjs fix ios apostrophe encoding
- javascript is int in array
- express middleware pass parameter
- arredondamento de numeros javascript
- stopper un intervalle javascript
- regex for email validation
- remove object from array by name javascript
- js loop over array of objects extract value
- react native add custom fonts
- angular run behind proxy
- html include js file flask
- how to pass state from parent to child in react
- make minutes have 0 in javascript