# Using 'For loop' to find the exact data

**URL:** <https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129>\
**Category:** Help Hub\
**Created:** [December 16, 2022, 6:20am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129 "2022-12-16T06:20:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-cos12](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@docking-module-cos12](https://community.postman.com/u/docking-module-cos12)\
**Post date:** [December 16, 2022, 6:20am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129/1 "2022-12-16T06:20:14Z")

</div>

_Query_ :  
I want to check all the email id and verify expected email id is present or not but in my cases for loop is not working. It would be great if anyone help me.

- Actual Response\* :

> {  
> “data”: [  
> {  
> “id”: 1,  
> “email”: “george.bluth@reqres.in”,  
> “first\_name”: “George”,  
> “last\_name”: “Bluth”,  
> },  
> {  
> “id”: 2,  
> “email”: “janet.weaver@reqres.in”,  
> “first\_name”: “Janet”,  
> “last\_name”: “Weaver”,  
> },  
> {  
> “id”: 3,  
> “email”: “emma.wong@reqres.in”,  
> “first\_name”: “Emma”,  
> “last\_name”: “Wong”,  
> },  
> {  
> “id”: 4,  
> “email”: “eve.holt@reqres.in”,  
> “first\_name”: “Eve”,  
> “last\_name”: “Holt”,  
> },  
> {  
> “id”: 5,  
> “email”: “charles.morris@reqres.in”,  
> “first\_name”: “Charles”,  
> “last\_name”: “Morris”,  
> },  
> {  
> “id”: 6,  
> “email”: “tracey.ramos@reqres.in”,  
> “first\_name”: “Tracey”,  
> “last\_name”: “Ramos”,  
> }  
> ],  
> }

_My Test_:

const jsonData = pm.response.json();  
console.log(jsonData);

for (let i=0; i\<=jsonData.data.length; i++)  
{

if(pm.expect(jsonData.data[i].email).to.eql(“charles.morris@reqres.in”))

{  
console.log(“Pass”);  
break;  
}

}

Error :-  
AssertionError: expected ‘george.bluth@reqres.in’ to deeply equal ‘charles.morris@reqres.in’

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [December 16, 2022, 10:40am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129/2 "2022-12-16T10:40:35Z")

</div>

It looks like your loop is working.

The problem is that you are asserting that the email address for all records equals Charles Morris.

As the first record is George Bluth, it fails on the very first record in the loop.

Not sure that is what you intended.

How are you determining the expected result?

Can you please confirm how you need to verify the email address.

If you just want to assert on whether Charles Morris exists somewhere in the response, then as your response seems to be an array, you can do this with the JavaScript find function and then assert on the results of the search.

Can you please use the preformatted text options when posting responses or code.

It’s really hard to read when everything is aligned to the left.

---

<div class="post-metadata">

**Author:** ![docking-module-cos12](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@docking-module-cos12](https://community.postman.com/u/docking-module-cos12)\
**Post date:** [December 16, 2022, 10:45am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129/3 "2022-12-16T10:45:28Z")

</div>

Hi @michaelderekjones ,  
Thanks for response. That particular email might come to any id. So i need to search all the results.  
I am new to postman . i thought using ‘for loop’ it will check all the values till it find the right one. can you please share the correct syntax.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [December 16, 2022, 10:54am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129/4 "2022-12-16T10:54:09Z")

</div>

That’s why you use the search instead of a loop.

You can search on the email address.

For example.

```auto
response = pm.response.json();

let search = (response.data.find(obj => {return obj.email === 'charles.morris@reqres.in'})).email; 

console.log(search); // charles.morris@reqres.in

pm.test("email = charles.morris@reqres.in", () => {
    pm.expect(search).to.eql("charles.morris@reqres.in");
});   

```

---

<div class="post-metadata">

**Author:** ![docking-module-cos12](https://avatars.discourse-cdn.com/v4/letter/d/41988e/32.png) [@docking-module-cos12](https://community.postman.com/u/docking-module-cos12)\
**Post date:** [December 16, 2022, 10:59am UTC](https://community.postman.com/t/using-for-loop-to-find-the-exact-data/42129/5 "2022-12-16T10:59:33Z")

</div>

> [@michaelderekjones](#):
>
> ```auto
> response = pm.response.json();
> 
> let search = (response.data.find(obj => {return obj.email === 'charles.morris@reqres.in'})).email; 
> 
> console.log(search); // charles.morris@reqres.in
> 
> pm.test("email = charles.morris@reqres.in", () => {
> pm.expect(search).to.eql("charles.morris@reqres.in");
> });
> 
> ```

Thank you !! it is working fine.
