# For...in loop inside a function

**URL:** <https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251>\
**Category:** JavaScript\
**Created:** [February 15, 2023, 5:20pm UTC](https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251 "2023-02-15T17:20:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuriAmancio](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@YuriAmancio](https://forum.codewithmosh.com/u/YuriAmancio)\
**Post date:** [February 15, 2023, 5:20pm UTC](https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251/1 "2023-02-15T17:20:49Z")

</div>

hi all,

in this exercise, Mosh asked to create a function and check if two objects areEqual, meaning with the same value in each key-value pair. In his solution, he checked each key-value pair individually. Is there a possibility to use a loop to check the key-value pairs?

```auto
const myAddress1 = new CreateAddress('e', 'f', 'g');
const myAddress2 = new CreateAddress('e', 'f', 'g');

function CreateAddress(street, city, zipcode) {
  this.street = street;
  this.city = city;
  this.zipCode = zipcode;
}

```

Below is what I am trying to do instead of doing each comparison separated using the && operator.

```auto
function areEqual(obj1, obj2) {
  for (let key of obj1) {
    for (let value of obj2) {
      return (obj1[key] === obj2[value])
    }
  }
}

console.log(areEqual(myAddress1, myAddress2))

```

But this is not working! Any implementation?

---

<div class="post-metadata">

**Author:** ![19jai](https://avatars.discourse-cdn.com/v4/letter/1/e99b99/32.png) [@19jai](https://forum.codewithmosh.com/u/19jai)\
**Post date:** [February 15, 2023, 10:22pm UTC](https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251/2 "2023-02-15T22:22:26Z")

</div>

Maybe it is because you are using a for…of loop instead a for…in loop for objects?

I am not taking this course, so I do not know what exactly Mosh’s question is asking. But from your address example, I assume that the objects you are comparing have the same properties or keys? If that is the case, then I think you don’t need the second for loop, as the key in obj1 can also be used for obj2. Here is what I will do using a for loop:

```auto
function checkEqualObjectsValues(obj1, obj2){
     let result = true;
     for(const key in obj1){
        if(obj1[key] !== obj2[key]){
           result = false;
           break
        }
     }
     return result;
}

console.log(checkEqualObjectsValues(myAddress1, myAddress2))

```

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [February 16, 2023, 5:09am UTC](https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251/3 "2023-02-16T05:09:37Z")

</div>

> [@YuriAmancio](#):
>
> if two objects areEqual

@19jai your solution has limitation for below test case

```auto
obj1 = {id:1, name:'azhar'}
obj2 = {id:1, name:'azhar', number:1234}

```

if we know that `obj1 and obj2` created using **`Map Object`** then simply

```auto
function checkEqualObjectsValues(obj1, obj2){
     for(const [key, value] of obj1){
        if(value !== obj2.get(key))
           return false;
     }
     return obj1.size === obj2.size;
}

```

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [February 16, 2023, 5:18am UTC](https://forum.codewithmosh.com/t/for-in-loop-inside-a-function/18251/4 "2023-02-16T05:18:45Z")

</div>

> [@YuriAmancio](#):
>
> `let key of obj1`

this peice of `code` will not work as we know that `obj1` is an **Map object** not **array**
