1var person1={first_name:"bob"};
2var person2 = {first_name:"bob"};
3
4//compare the two object
5if(JSON.stringify(person1) === JSON.stringify(person2)){
6 //objects are the same
7}
8
1function isEquivalent(a, b) {
2 // Create arrays of property names
3 var aProps = Object.getOwnPropertyNames(a);
4 var bProps = Object.getOwnPropertyNames(b);
5
6 // If number of properties is different,
7 // objects are not equivalent
8 if (aProps.length != bProps.length) {
9 return false;
10 }
11
12 for (var i = 0; i < aProps.length; i++) {
13 var propName = aProps[i];
14
15 // If values of same property are not equal,
16 // objects are not equivalent
17 if (a[propName] !== b[propName]) {
18 return false;
19 }
20 }
21
22 // If we made it this far, objects
23 // are considered equivalent
24 return true;
25}
1function shallowEqual(object1, object2) {
2 const keys1 = Object.keys(object1);
3 const keys2 = Object.keys(object2);
4
5 if (keys1.length !== keys2.length) {
6 return false;
7 }
8
9 for (let key of keys1) {
10 if (object1[key] !== object2[key]) {
11 return false;
12 }
13 }
14
15 return true;
16}
1//No generic function to do that at all.
2//use Lodash, famous for such must have functions, all efficiently built
3//http://lodash.com/docs#isEqual ---
4
5npm install lodash
6
7_.isEqual(object, other);
1var myString = "Item1";
2
3var jsObject =
4{
5 Item1:
6 {
7 "apples": "red",
8 "oranges": "orange",
9 },
10 Item2:
11 {
12 "bananas": "yellow",
13 "pears": "green"
14 }
15};
16
17var keys = Object.keys(jsObject); //get keys from object as an array
18
19keys.forEach(function(key) { //loop through keys array
20 console.log(key, key == myString)
21});