I have a JSON response from MongoDB in this format:
[{
"_id" : 4,
"status" : [
{
"status" : "Closed",
"count" : 2
},
{
"status" : "Open",
"count" : 17
}
],
"count" : 19
},
{
"_id" : 3,
"status" : [
{
"status" : "Closed",
"count" : 6
},
{
"status" : "Open",
"count" : 22
}
],
"count" : 28
}]
I want to manipulate this into an array of objects so it becomes easy for me to pass them onto the highcharts.
categories: ['3', '4']
series: [{
name: 'Open',
data: [22,17]
}, {
name: 'Closed',
data: [6,2]
}]
Basically I want to group all the "open" and "closed" with respect to the categories.
If your data is always formatted like this i would do something like this:
var data = [{ "_id" : 4, "status" : [ { "status" : "Closed", "count" : 2 }, { "status" : "Open", "count" : 17 } ], "count" : 19 }, { "_id" : 3, "status" : [ { "status" : "Closed", "count" : 6 }, { "status" : "Open", "count" : 22 } ], "count" : 28 }] $(function () { var myCategories = []; var mySeries = [{ name: 'Open', data: [] },{ name: 'Closed', data: [] }]; for (var i = 0; i < data.length; i++){ myCategories.push(data[i]._id); mySeries[0].data.push(data[i].status[0].count); mySeries[1].data.push(data[i].status[1].count); } $('#series').text(JSON.stringify(mySeries)); $('#categories').text(JSON.stringify(myCategories)); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>Categories:</label> <div id="categories"></div> <label>Series:</label> <div id="series"></div>
Then in highcharts, simply set
categories: myCategories,
series: mySeries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.