Within my array of objects (referred to as "data"), the first two indexes are outlined below:
0: Object
granularity_time: "total"
granularity_geo: "nation"
location_code: "norge"
border: "2020"
age: "90+"
sex: "female"
year: "1900"
week: "1"
yrwk: "1900-01"
season: "1899/1900"
x: "24"
date: "1900-01-01"
n: "219"
date_of_publishing: "2022-01-12"
tag_outcome: "death"
1: Object
granularity_time: "total"
granularity_geo: "nation"
location_code: "norge"
border: "2020"
age: "90+"
sex: "male"
year: "1900"
week: "1"
yrwk: "1900-01"
season: "1899/1900"
x: "24"
date: "1900-01-01"
n: "127"
date_of_publishing: "2022-01-12"
tag_outcome: "death"
The data differentiates between men and women within the same age group, organized in pairs of objects. For instance, index 0 represents women aged 90+, while index 1 signifies men aged 90+. This pattern continues with index 2 for women aged 80+, index 3 for men aged 80+, and so on.
To reorganize this data structure such that each index contains a pair of objects representing men and women in the same age group, one could format it similarly to the following code snippet:
const newData = [
{
woman: data[0],
men: data[1]
},
{
woman: data[2],
men: data[3]
},
{
woman: data[4],
men: data[5]
},
{
woman: data[6],
men: data[7]
},
{
woman: data[8],
men: data[9]
},
{
woman: data[10],
men: data[11]
},
{
woman: data[12],
men: data[13]
}
];
An attempted function was made to iterate over the data and implement this desired formatting. However, the entries turned out undefined rather than paired correctly. Below is the function in question:
const formatData = (data) => {
const newData = [];
data?.map((item, i) => {
i % 2 === 0 ? newData.push({ woman: item[i], men: item[i++] }) : null;
});
return newData;
};
I am seeking guidance on what might be missing or incorrect within the function. Any insights would be greatly appreciated.