# Comparing objects

**URL:** <https://forum.kirupa.com/t/comparing-objects/643750>\
**Category:** programming\
**Created:** [August 2, 2020, 3:41pm UTC](https://forum.kirupa.com/t/comparing-objects/643750 "2020-08-02T15:41:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Woodenshipandironman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woodenshipandironman/32/12138_2.png) [@Woodenshipandironman](https://forum.kirupa.com/u/Woodenshipandironman)\
**Post date:** [August 2, 2020, 3:41pm UTC](https://forum.kirupa.com/t/comparing-objects/643750/1 "2020-08-02T15:41:10Z")

</div>

I’m looking for a function that compares two values. These values may or may not be objects. If they happen to be objects, I expect this function to verify that both objects have the same number of properties and that each property is contained in both. Lastly the function calls itself to evaluate each pair of properties. If they hold different values the function should return false.

```
let obj1 = {
	a: 1,
	b: "2",
	c: { d: 2 }
};

let obj2 = {
	a: 1,
	b: "2",
	c: { d: 2 }
};

let obj3 = {
	a: 0,
	b: "2",
	c: { d: 2 }
};

let obj4 = {
	a: 0,
	b: "2",
	c: { 
		d: 2,
		e: { f: "A" }
	}
};

function deepEqual(a, b) {
	if (typeof a == 'object' && a != null && typeof b == 'object' && b != null) {
		if (Object.keys(a).length == Object.keys(b).length) {
			for (let i = 0; i < Object.keys(a).length; i += 1) {
				if (Object.keys(b).includes(Object.keys(a)[i])) {
					console.log(Object.keys(a)[i] + ": " + a[Object.keys(a)[i]] + ", " + Object.keys(a)[i] + ": " + b[Object.keys(b)[i]]);
					deepEqual(a[Object.keys(a)[i]], b[Object.keys(a)[i]]);
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	} else if (a === b) {
		return true;
	} else {
		return false;
	}
}

```

Calling `deepEqual` with any pair of aforementioned objects doesn’t have the expected result. For starters the function returns `undefined` and never seems to reach the majority of the `if (...) else` blocks. I’m not looking for a solution but perhaps a hint that might indicate what went wrong.

What works:

Calling `deepEqual` on arbitrary values.

```
deepEqual(3, 4)
false
deepEqual(3, 3)
true

```

Recursion works too. If I pass `deepEqual` objects containing nested objects, `console.log` outputs their respective values.

```
deepEqual(obj1, obj2)
a: 1, a: 1
b: 2, b: 2
c: [object Object], c: [object Object]
d: 2, d: 2
```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 2, 2020, 4:11pm UTC](https://forum.kirupa.com/t/comparing-objects/643750/2 "2020-08-02T16:11:54Z")

</div>

Do you want to know whats wrong with your function? Or just want the functionality? If the latter, [lodash](https://lodash.com) has an [isEqual()](https://lodash.com/docs/#isEqual) you can use.

---

<div class="post-metadata">

**Author:** ![Woodenshipandironman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woodenshipandironman/32/12138_2.png) [@Woodenshipandironman](https://forum.kirupa.com/u/Woodenshipandironman)\
**Post date:** [August 2, 2020, 4:15pm UTC](https://forum.kirupa.com/t/comparing-objects/643750/3 "2020-08-02T16:15:45Z")

</div>

Yeah, I’m not sure why it returns `undefined` instead of either `true` or `false` after looping over an object’s properties. I’m in the process of learning so I figure it’s best for me if I wrote my own stuff. Actually this is an exercise in Eloquent Javascript and despite there being solutions online, I won’t be able to sleep at night if I cheat. I already feel guilty asking for assistance online lol.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [August 2, 2020, 4:22pm UTC](https://forum.kirupa.com/t/comparing-objects/643750/4 "2020-08-02T16:22:35Z")

</div>

If you’d like a hint, look through all your if-else blocks and figure out which one isn’t returning anything. Then you’ll know where your undefined is coming from 😉

---

<div class="post-metadata">

**Author:** ![Woodenshipandironman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woodenshipandironman/32/12138_2.png) [@Woodenshipandironman](https://forum.kirupa.com/u/Woodenshipandironman)\
**Post date:** [August 2, 2020, 4:40pm UTC](https://forum.kirupa.com/t/comparing-objects/643750/5 "2020-08-02T16:40:19Z")

</div>

Fixed it, thanks. Turns out I don’t know what I’m doing. Back to the drawing board lol
