# How to find the last object in the array

**URL:** <https://community.postman.com/t/how-to-find-the-last-object-in-the-array/32504>\
**Category:** Help Hub\
**Tags:** array\
**Created:** [February 11, 2022, 3:01pm UTC](https://community.postman.com/t/how-to-find-the-last-object-in-the-array/32504 "2022-02-11T15:01:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aieshachayka](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@aieshachayka](https://community.postman.com/u/aieshachayka)\
**Post date:** [February 11, 2022, 3:01pm UTC](https://community.postman.com/t/how-to-find-the-last-object-in-the-array/32504/1 "2022-02-11T15:01:18Z")

</div>

Hi!  
In the below example I have API that will return list of sections, each section can contain some amount of Shows. First section title will be always “All Upcoming Shows”, and last section will always be “All Replay Shows”. There can be many different sections in-between them, with different names, but first and last sections should always have names “All Upcoming Shows” and “All Replay Shows”. So, I want to check that sections will have those names. For the first section it is pretty simple (

```auto
pm.test("First Category is 'All Upcoming Shows'", () => {
    const responseJson = pm.response.json();
    pm.expect(responseJson.items[0].sectionTitle).to.eql('All Upcoming Shows');
});

```

)  
but how to indicate the last “sectionTitle” in the array if it can be different amount of sections all the time?  
Please, help me with the solution.

```auto
{
    "items": [
        {
            "sectionTitle": "All Upcoming Shows",
            "shows": [
                {
                    "id": "12345"
                },
                {
                    "id": "12346"
                }
            ]
        },
		{
            "sectionTitle": "Title1",
            "shows": [
                {
                    "id": "12345"
                },
                {
                    "id": "12347"
                }
            ]
        },
		{
            "sectionTitle": "Title2",
            "shows": [
                {
                    "id": "12348"
                },
                {
                    "id": "12349"
                }
            ]
        },
		{
            "sectionTitle": "All Replay Shows",
            "shows": [
                {
                    "id": "12343"
                },
                {
                    "id": "12345"
                }
            ]
        }
		],
		"serverTimestamp": "2022-02-11T14:03:27.328Z"
		}

```

---

<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:** [February 11, 2022, 4:19pm UTC](https://community.postman.com/t/how-to-find-the-last-object-in-the-array/32504/2 "2022-02-11T16:19:52Z")

</div>

Hey @aieshachayka

You could use the `_.first()` and `_.last()` [Lodash](https://lodash.com/docs/4.17.15) methods to find those in the response array.

```javascript
const responseJson = pm.response.json();

pm.test("First Category is 'All Upcoming Shows'", () => {
    pm.expect(_.first(responseJson.items).sectionTitle).to.eql('All Upcoming Shows');
});

pm.test("Last Category is 'All Replay Shows'", () => {
    pm.expect(_.last(responseJson.items).sectionTitle).to.eql('All Replay Shows');
});

```

You could also use this to get the last item in the array:

```auto
pm.test("Last Category is 'All Replay Shows'", () => {
    pm.expect(responseJson.items[responseJson.items.length - 1].sectionTitle).to.eql('All Replay Shows');
});

```

---

<div class="post-metadata">

**Author:** ![aieshachayka](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@aieshachayka](https://community.postman.com/u/aieshachayka)\
**Post date:** [February 18, 2022, 3:12pm UTC](https://community.postman.com/t/how-to-find-the-last-object-in-the-array/32504/3 "2022-02-18T15:12:04Z")

</div>

thank you so much! works perfectly!
