How to use loop in tests section?

I have a response which is a combination of 10 documents and each document will have few unwanted attributes like name, ts, time which should be deleted and the deleted response should be stored as variable in environment file. So, is there any method to fulfill this?
For example, when I am having only one document in response, I am doing in the below manner
let response = pm.response.json()
const responseA= response.Documents[0]
delete responseA[0].name;
delete responseA[0]._ts;
delete responseA[0].time;

But this is not feasible if the document count is huge.

Hey @Harsha,
Welcome to the community :wave:
Let’s say the response returns 10s, 100s of documents as an array, then:

pm.response.json().documents.map({name, _ts, time, ...rest} => {
 pm.variables.set("key", {name, _ts, time}) // set the deleted fields in a variable
 return rest; //this will return the other fields besides the ones you want to delete
})

We need some javascript in the Tests tab to write any custom logic. You could use for-loops as well and other ways, I’m using ES6 syntax in the snippet above, because it’s my personal preference and less verbose but declerative. But you’re free to write a similar thing in an iterative fashion as long as it fulfills your requirement and is easy for your team to work with, I guess :grinning_face_with_smiling_eyes:

Hi Meenakshi,

I am getting syntax error when I used the above code in my Postman script

Hey @Harsha ,
Would it be possible for you to share the code you’re working on via a public workspace? I could easily replicate it then and raise a pull request or point the error?

Won’t take long, I promise. Let’s listen to @hannah.neil - Using Public Workspaces to Get Help with Postman

Now that I look at it I think the arrow is missing.
It’s supposed to be:

array.map({obj}=>{
//action
})

this => is missing

Thanks Meenakshi. I am using another solution like using for loop and delete the unwanted attributes.