// parse to contantInfo array
const response = pm.response.json().contantInfo;
// filter first
let tracks = response.filter(obj => obj.track != "Software Testing");
console.log(tracks);
// then return ID's
let IDs = tracks.map(item => item.id);
console.log(IDs);
// This can be done in one line
let IDsV2 = response.filter(obj => obj.track != "Software Testing").map(item => item.id);
console.log(IDsV2);
thank you it’s work
can i ask you another question
i wanna make patch request after filtration ,console don’t give me error but it’s not modified
i write this code in another request in pre_rquest
const IDsV2=pm.environment.get("IDsV2");
console.log(IDsV2);
for (let i = 0; i < IDsV2.length; i++) {
let value=IDsV2[i];
const PatchRequest = {
url: 'http://localhost:3000/contantInfo/'+value,
method: 'PATCH',
body: {
"certifications": "not requried"
}
};
pm.sendRequest(PatchRequest, (error, response) => {
if (error) {
console.log(error);
}
});
}
Does the URL look correct? Is the Id being added to the end as expected.
If it is, then it will be something with how the API is handling the patch request. (Which I can’t answer for you as I don’t know anything about the API and what its expecting).
To test the patch request on its own, create a single patch request with the values hard coded and see if that works.
When you console log IDsV2 on the second line, is it actually returning an array?
You usually have to stringify() arrays when storing them as a collection or environment variables, and JSON.parse() them when retrieving or they will just be treated as a string.
On a side note, why can’t this just be added to the end of the code in the Tests tab so you don’t have to save it to an environment variable at all. Is it being used in another request?
Finally, although you can use a for loop. As the data is an array you can also use a forEach loop instead which is slightly easier to handle. You also don’t need to declare the value like you are doing. You can just replace “value” with “i” as the iteration data is already available in that variable.