5
I’m having trouble grouping some data using the Javascript.
I have the following array of objects that return from database and shipping to the view:
var object = [
{ data: 1, categories: "Branca", name: "Feminino" },
{ data: 1, categories: "Parda", name: "Masculino" },
{ data: 2, categories: "Branca", name: "Masculino" },
];
From it, I need to generate two more arrays grouped, to send to a chart on Highcharts.
I would like to arrive at the following result:
categories: ["Parda", "Branca"]
series: [{
name: "Masculino",
data: [1, 2]
}, {
name: "Feminino",
data: [0, 1]
}]
I’ve tried some possibilities, I’ve done the array of Categories, was very simple, but the array of series that complicated.
Could someone help me? Thanks in advance!
Why does Feminine need to have the dates 0 and 1? What is the logic of the dates?
– Andre
The logic of the dates follows the order of the categories. Ex: Looking through the original array, the masculine when Parda, has the date 1, and when White the date 2. Whereas the Feminine has no value when Parda, so its date is 0, and has when White the date 1.
– Tácio Brito
You can take a look at this link that might help you: Groupby And I think this post I found might help a lot: https://stackoverflow.com/questions/21776389/javascript-object-grouping
– Jonatas Rodrigues