# Run the same test through all arrays objects

**URL:** <https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782>\
**Category:** Help Hub\
**Created:** [November 4, 2022, 1:04pm UTC](https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782 "2022-11-04T13:04:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [November 4, 2022, 1:04pm UTC](https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782/1 "2022-11-04T13:04:46Z")

</div>

Hey!

Is there a way to run the same test script through all array objects?

 ![Screenshot 2022-11-04 at 12.58.18](https://us1.discourse-cdn.com/getpostman/original/3X/e/a/ea5ea1c5957ddb4fb5dbc68482638dcf6dceac33.png)

I have that pm.test that i want to run through all array objects, that in that case is 82, but it might be more or it might be less.

Is there a way to this?  
Thanks a bunch!

---

<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:** [November 4, 2022, 2:40pm UTC](https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782/2 "2022-11-04T14:40:06Z")

</div>

pm.response.json() will most likely produce a JavaScript object, not an array.

It’s an important distinction as an ForEach loop only works with arrays.

Therefore, you need to wrap a for loop around your test.

In the following example(s). I have created an array, and I’ve included examples of both loops. I suspect you will need to use the first example.

```auto
var array = 
    [{
    "id": 377,
    "forename": "Forename 493068",
    },
    {
    "id": 376,
    "forename": "Forename 493067",
    },
    {
    "id": 375,
    "forename": "Forename 123456",
}];

console.log(typeof(array)); // array is a special type of "object"

pm.test("Array", () => {
    pm.expect(array).to.be.an("array"); // change this to object and it will fail
});

for (let i = 0; i < array.length; i++) {

    var id = array[i].id;
    // console.log(id);
    
    pm.test(`for loop expect ${id} to be an number`, () => {
        pm.expect(id).to.be.an("number");
    });

};

array.forEach(line => {

    var id = line.id;
     
    pm.test(`forEach loop expect ${id} to be an number`, () => {
        pm.expect(id).to.be.an("number");
    });
    
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/8/e8e2471e641b0e80de01b29ea52c15519af5a19b.png)

---

<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:** [November 4, 2022, 2:48pm UTC](https://community.postman.com/t/run-the-same-test-through-all-arrays-objects/40782/3 "2022-11-04T14:48:27Z")

</div>

I don’t quite understand why you are setting and retrieving a global variable within the same bit of code.

Appears superfluous unless you are using that variable in another request which considering you want to look through 80+ elements seems unlikely.

Why can’t you just define the variable direct from the response.

Also, does it really need to be a global variable available to all collections.

I suspect if needed at all, it should be set as a collection or environment variable.
