In my project I've requirement to make a rest API
call and fetch a JSON
array in response.
Each array element will contain a item 'key' (project key). Response example is here.
Now I want to loop over this array, fetch project key, then a make another REST call to fetch details of each project.
I'm using below code to do this, but final output is not coming as expected.
var myOutput = [];
$.when (
$.getJSON(projectListURL, function (data) {
var compArr = data.components;
for (i=0 ; i<compArr.length ; i++) {
var projectKey = compArr[i].key;
var projectName = compArr[i].name;
var myURL = "http://myserver.com/projectdetails?projectkey=" + projectKey;
$.getJSON(myURL, function (data) {
}).then (function (data){
console.log("myURL:" + myURL);
console.log("name:" + projectName);
var item = {};
item["name"] = projectName;
item["status"] = data.projectStatus.status;
console.log(item);
myOutput.push(item);
});
}
})
).done (function () {
console.log(myOutput);
});
I want myOutput to be something like:
[
{name: "BaseProj", status: "OK"},
{name: "Sudoku Project", status: "ERROR"},
{name: "My Project", status: "WARN"}
]
But I'm getting:
[
{name: "My Project", status: "OK"},
{name: "My Project", status: "ERROR"},
{name: "My Project", status: "WARN"}
]
Please suggest what should be the correct approach?
Thank You