# Help me to save id of an object having a certain name

**URL:** <https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [July 25, 2024, 9:45am UTC](https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541 "2024-07-25T09:45:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leveralex](https://avatars.discourse-cdn.com/v4/letter/l/e79b87/32.png) [@Leveralex](https://community.postman.com/u/Leveralex)\
**Post date:** [July 25, 2024, 9:45am UTC](https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541/1 "2024-07-25T09:45:58Z")

</div>

Hello,

I have a response like this:

```auto
{
	"documents": [
		{
			"id": 1,
			"name": "document1"
		},
		{
			"id": 3,
			"name": "document3"
		},
		{
			"id": 2,
			"name": "document2"
		},
		...
	]
}

```

The order of objects in “documents” can be different for any new request.  
So, I need to do something like:

if “name” = “document3” then set variable “documentId3” = “3”,  
if “name” = “document1” then set variable “documentId1” = “1”  
and etc. for each document from the response.

Finally I need to see in variables:  
documentId1 = id of the document with name “document1”  
documentId2 = id of the document with name “document2”  
and so on.

How can I do it?

---

<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:** [July 25, 2024, 11:43am UTC](https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541/2 "2024-07-25T11:43:34Z")

</div>

Hey @Leveralex 👋

Welcome to the Postman Community! :postman:

You could use the JS function [.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to locate those objects and then save the `id` from the result.

```auto
let resp = pm.response.json();
let document1 = resp.documents.find(({ name }) => name === "document1");

pm.environment.set('document1', document1.id);

```

I’m sure you could replicate that for the other objects to save the values from the response.

---

<div class="post-metadata">

**Author:** ![Leveralex](https://avatars.discourse-cdn.com/v4/letter/l/e79b87/32.png) [@Leveralex](https://community.postman.com/u/Leveralex)\
**Post date:** [July 25, 2024, 4:09pm UTC](https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541/3 "2024-07-25T16:09:45Z")

</div>

@danny-dainton Thank you! It works 🙂

---

<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:** [July 28, 2024, 4:09pm UTC](https://community.postman.com/t/help-me-to-save-id-of-an-object-having-a-certain-name/64541/4 "2024-07-28T16:09:46Z")

</div>

Closed on July 28, 2024, 4:09pm UTC

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