# Find the right object in array and assert on it

**URL:** <https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [April 5, 2022, 5:27pm UTC](https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244 "2022-04-05T17:27:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![altimetry-physicist2](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@altimetry-physicist2](https://community.postman.com/u/altimetry-physicist2)\
**Post date:** [April 5, 2022, 5:27pm UTC](https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244/1 "2022-04-05T17:27:58Z")

</div>

Hi there! It will be a noob question and I’m so sorry for it. But I cannot find any way of doing it on my own, so I thought I could use some help.

So, if I have a JSON response and there is some object in the array, how could I assert on it if I don’t know its index?

Let’s say that it’s the response that I want to test:

```auto
{
    "users": [
        {
            "name": "Julius",
            "pet": "iguana"
        },
        {
            "name": "Bob",
            "pet": "dog"
        },
        {
            "name": "Dave",
            "pet": "cat"
        }
    ]
}

```

I need to verify if user ‘Julius’ has an ‘iguana’ pet.

If I would know Julius’s index, then I would check his pet this way:

```auto
pm.test("Julius got a pet iguana", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.users[0].pet).to.eql("iguana")
});

```

But unfortunately, I don’t know what his index is. I guess that I need to do something to find the index of ‘Julius’ record first, so I could assert on him and his pet. How could I do it?

I kept reading about some find methods but I didn’t find any examples that would help me with understanding how to use those.

I’ll be much obliged for any clues!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 5, 2022, 6:01pm UTC](https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244/2 "2022-04-05T18:01:04Z")

</div>

You could use something like:

```auto
let obj = pm.response.json().users.find(e => e.name === 'Julius');
console.log(obj.pet);

```

You could use that function inside a test and expect against `obj.pet` to equal `iguana`.

For example:

```auto
pm.test('Name of pet matches', () => {
    let obj = pm.response.json().users.find(e => e.name === 'Julius');
    pm.expect(obj.pet).to.eql('iguana');    
});
```

---

<div class="post-metadata">

**Author:** ![altimetry-physicist2](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@altimetry-physicist2](https://community.postman.com/u/altimetry-physicist2)\
**Post date:** [April 6, 2022, 9:13am UTC](https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244/3 "2022-04-06T09:13:45Z")

</div>

That’s it, it solved my issue. Thank you so much, Danny! 🙂
