I have a JSON response that looks like this
[
{
"alias": "Server1",
"hostgroup_worst_service_state": "0",
"status_history": {
"status": [
"0",
"2",
"0",
"0"
]
}
},
{
"alias": "Server2",
"hostgroup_worst_service_state": "0",
"status_history": {
"status": [
"0",
"0",
"0",
"0"
]
}
},
{
"alias": "Server3",
"hostgroup_worst_service_state": "0",
"status_history": {
"status": [
"1",
"1",
"1",
"0"
]
}
}
]
I can easily access the alias and hostgroup_worst_state using the .each iterator function:
$.each(data, function(i, item) {
console.log(item.alias)
console.log(item.hostgroup_worst_service_state)
}
However I struggle to have access to the status data. I would need to display them this way:
["0","2","0","0"]
["0","0","0","0"]
["1","1","1","1"]
These value will next be used to display graphs using jQuery Sparklines.
Thanks for your help,
You have an array of objects, where each object has a property called
status_history, which points to another object that has a property calledstatus, which contains your required array. Thus, to access this array, use the following:Note that
thisinside.each()refers to each object/value you iterate. It’s the equivalent of using the following:Also, if you want an array of arrays with the given status data, you could use
$.map():