//set object index as variables
var jsonData = pm.response.json();
var index = jsonData.findIndex(x => x.title==="Canary");
console.log(index);
pm.collectionVariables.set("schoolIndex", index);
//set schooleID with found index as variables
pm.collectionVariables.set("schoolId", jsonData[index].id);
[
{
"id": 2,
"title": "School 2",
"foundationDate": "2021-10-30T00:00:00+03:00",
"terminatedDate": null,
"status": 1,
"coursesCount": 0
},
{
"id": 3,
"title": "Bitchip",
"foundationDate": "2010-04-10T00:00:00+03:00",
"terminatedDate": "2020-04-10T00:00:00+03:00",
"status": 1,
"coursesCount": 0
}
]
//2nd way to get object from array
function findByName(jsonData) {
for(let item of jsonData) {
if(item.title == pm.collectionVariables.get("schoolTitle")) {
return item;
}
}
}
console.log(findByName(jsonData));
var itemData = findByName(jsonData);
pm.environment.set("2ndId", itemData.id);
//2nd way to get object Index from array
function findObjectIndex(jsonData) {
for(const [index, item] of jsonData.entries()) {
if(item.title == pm.collectionVariables.get("schoolTitle")) {
return index;
}
}
}
console.log(findObjectIndex(jsonData));
var itemIndex = findObjectIndex(jsonData);
pm.environment.set("Index", itemIndex);