0

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.

0

1 Answer 1

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much Juan, this answers my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.