# Find item in array by specific properties

**URL:** <https://community.postman.com/t/find-item-in-array-by-specific-properties/56250>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [January 5, 2024, 11:53pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250 "2024-01-05T23:53:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ignleotta](https://avatars.discourse-cdn.com/v4/letter/i/6f9a4e/32.png) [@ignleotta](https://community.postman.com/u/ignleotta)\
**Post date:** [January 5, 2024, 11:53pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/1 "2024-01-05T23:53:53Z")

</div>

Hi folks, I am vey new to postman. I have an array as response and I want to check if a specific item is present. I can find the item by its properties. I see in all the similar topics it is possible to use find() but I cannot make it work. May you please help me?  
here it is an example of the response bdy:

```auto
[
    {
        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "sdfgh",
        "isPrimary": false
    },
    {
        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "dfsd",
        "isPrimary": false
    },
    {
        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "Chuck Norris",
        "isPrimary": true
    }
]

```

Let’s say I want to verify the item with saleperson===“Chuck Norris is present” and it has IsPrimary set to true . How can I do?

---

<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:** [January 6, 2024, 10:34am UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/2 "2024-01-06T10:34:17Z")

</div>

Hey @ignleotta 👋

Welcome to the Postman community! :postman:

These type of questions are quite common and fall more on the JavaScript side than being a specific Postman problem.

It’s always good to do a search across the forum first to check for similar questions.

Here’s on response I gave a while back the you can use here:

> [@Find the right object in array and assert on it](https://community.postman.com/t/find-the-right-object-in-array-and-assert-on-it/34244/2):
>
> You could use something like: 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: pm.test('Name of pet matches', () =\> { let obj = pm.response.json().users.find(e =\> e.name === 'Julius'); pm.expect(obj.pet).to.eql('iguana'); });

You would need to change the references to your context but it will work the same. Give it a go first and then come back if you have any issues.

---

<div class="post-metadata">

**Author:** ![ignleotta](https://avatars.discourse-cdn.com/v4/letter/i/6f9a4e/32.png) [@ignleotta](https://community.postman.com/u/ignleotta)\
**Post date:** [January 6, 2024, 1:53pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/3 "2024-01-06T13:53:02Z")

</div>

@danny-dainton I read mean posts already and used a solution similar to what you proposed but unfortunately i also returns:  
TypeError: Cannot read properties of undefined (reading ‘find’)

---

<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:** [January 6, 2024, 3:07pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/4 "2024-01-06T15:07:03Z")

</div>

Without seeing what you have in the script, I’d going to be difficult to solve that.

It’s probably just a reference issue as that other question would have had a different response structure. You would need to edit that references to match your structure.

---

<div class="post-metadata">

**Author:** ![ignleotta](https://avatars.discourse-cdn.com/v4/letter/i/6f9a4e/32.png) [@ignleotta](https://community.postman.com/u/ignleotta)\
**Post date:** [January 6, 2024, 3:39pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/5 "2024-01-06T15:39:27Z")

</div>

At the end I found a solution without using find or filter:

```auto
pm.test('ResposehasAPrimary', function() {
_.each(pm.response.json(), (item) => {
    if(item.salePerson === 'Chuck Norris') {
        console.log(item);
     pm.expect(item.isPrimary=== true);
    }
});
})

```

Thanks @danny-dainton for his answer to another post which I used to find this solution.

---

<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:** [January 6, 2024, 4:29pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/6 "2024-01-06T16:29:43Z")

</div>

Wouldn’t you want your assertion to be something like:

```auto
pm.expect(item.isPrimary).to.be.true;

```

I’m not sure that yours would be working as expected.

---

<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:** [January 7, 2024, 3:26pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/7 "2024-01-07T15:26:43Z")

</div>

Here are two ways to test for those conditions using the JavaScript find function.

Option 1 searches on both conditions, and for the test you just need to check that the search is not undefined.

Option 2 searches for the name, and the test then checks if the isPrimary key = true in the returned object.

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

console.log(response);

pm.test('Sales person = Chuck Norris and isPrimary = true V1', () => {
    let searchV1 = response.find(obj => obj.salePerson === "Chuck Norris" && obj.isPrimary === true);
    console.log(searchV1);
    pm.expect(searchV1).to.not.be.undefined
})

pm.test('Sales person = Chuck Norris and isPrimary = true V2', () => {
    let searchV2 = response.find(obj => obj.salePerson === "Chuck Norris");
    console.log(searchV2);
    pm.expect(searchV2.isPrimary).to.be.true;
})

```

Good practice is to make your test fail to ensure that you are not getting a false positive. For example, change the “to.be.true” to “to.not.be.true” and ensure it fails the test. You should do this with all assertions. Make it pass, then make it fail, before setting it back to pass. This way, you know the assertion is working as expected.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 6, 2024, 3:27pm UTC](https://community.postman.com/t/find-item-in-array-by-specific-properties/56250/8 "2024-02-06T15:27:19Z")

</div>

Closed on February 6, 2024, 3:27pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
