'Javascript json chart sorting by specific object key
I have the following JSON that I would like to work within data analysis using javascript
{
{
"Id": 17,
"BeneID": "",
"Age": "",
"Business": "",
"Centre": "",
"Created_date": "2022-02-04 00:00:00",
"Created_time": "11:09:38",
"Datejoin": "2021-11-10 00:00:00",
"Gender": "female",
"ImageUrl": "",
"Name": "",
"Region":"Region1",
},
{
"Id": 17,
"BeneID": "",
"Age": "",
"Business": "",
"Centre": "",
"Created_date": "2022-02-04 00:00:00",
"Created_time": "11:09:38",
"Datejoin": "2021-11-10 00:00:00",
"Gender": "female",
"ImageUrl": "",
"Name": "",
"Region":"Region2",
},
}
each object is represented the same way but with a dynamic value i.e region, name, gender is dynamic for each object.
I would like to present the data for statistical purposes such as analyzing the data, for instance, I wanted to map for each object the region and number of males and females registered
I tried the following
var groupedRegions = {};
const url = "/datajson";
fetch(url)
.then(response => response.json())
.then(data => {
data.response.forEach((a) => {
groupedRegions[a.Region] = groupedRegions[a.Region] || [];
groupedRegions[a.Region].push({
...(a.Gender === "female" ? { female: a.Gender } : {}),
...(a.Gender === "male" ? { male: a.Gender } : {}),
})
$.each(groupedRegions, function(i, item) {
$('<tr class="dbregionrow">').append(
$('<td>').text(item),
$('<td>').text(item[0].female.length),
$('<td>').text(item[0].male.length),
).appendTo('.table');
});
});
});
the expected result was to loop the entire JSON and grab all regions with specific gender ;
Region NoFemales NoMales
Region1 10 10
Region2 10 10
or how can I easily group the data by region or by any field in the JSON object?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
