I am working with a json file that contains similar data sets but different objects.
{
"AP": [{
"name": "Autogen Program"
}, {
"status": "Completed"
}, {
"start": "2014-05-05"
}, {
"end": "2014-05-05"
}],
"BP": [{
"name": "Backend Program"
}, {
"status": "In Progress"
}, {
"start": "2014-05-05"
}, {
"end": ""
}],
"AP": [{
"name": "Capital Program"
}, {
"status": "Pending"
}, {
"start": ""
}, {
"end": ""
}]
}
I have developed a function to individually call each object.
$(function() {
$.getJSON('http://localhost:8080/GChartServlet/data1.json', function(statusDataSet) {
$.each(statusDataSet.AP, function(i, f) {
var color;
switch(f.status) {
case "In Progress":
color = "yellow";
break;
case "Pending":
color = "red";
break;
case "Completed":
color = "green";
break;
}
$("#stat1").append(f.value).css('background-color', color);
});
});
});
I am wondering if it is possible to retrieve all the status values using a single function. Or, how can I separate the color case logic into a separate function in order to avoid repeating the switch statement for each function?
Maybe something like this:
$("#stat1").append(AP.status).css('background-color', color);
$("#stat2").append(BP.status).css('background-color', color);