Using below code, I am able to group objects from my existing data with Underscore JS as shown.
{Group1: Array[10], Group2: Array[13], Group3: Array[16], Group4: Array[21], Group5: Array[38]}
//Create a category based on group assigned
var groupedData = _.groupBy(results, function (d) {
return d.groups;
});
console.log(groupedData);
But what I really need is to turn the above into JSON array looking like:
var myData = [["Group1", 10], ["Group2",13], ["Group3",16], ["Group4",21], ["Group5",38]];
How can I adjust my code in order to get the desired result?
Thank you for your help.
Use Array.map and Object.keys
var results = { a: [1, 2], b: [2, 3] }; var grouped = Object.keys(results).map(function(key) { return [key, results[key].length]; }); document.body.innerHTML = JSON.stringify(grouped);
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.