I have successfully implemented a code to group objects by their category. Here is the code snippet:
let groupBy = (element, key) => {
return element.reduce((value, x) => {
(value[x[key]] = value[x[key]] || []).push(x);
return value;
}, {});
};
let items = groupBy(results, 'category')
The resulting grouped objects look like this:
{
"Administration": [
{
"shp_name": "Village Boundary",
"color_prop": "#000000",
"shp_prop": "Batu Ampar"
},
{
"shp_name": "Village Boundary",
"color_prop": "#FFFFFF",
"shp_prop": "Sungai Jawi"
}
],
"Land_use": [
{
"shp_name": "Land Use 2019",
"color_prop": "#000000",
"shp_prop": "Grassland"
},
]
}
Now, I need help in merging the color_prop
and shp_prop
into arrays within each object. The desired output should be like this:
{
"Administration": [
{
"shp_name": "Village Boundary",
"color_prop": ["#000000","#FFFFFF"],
"shp_prop": ["Batu Ampar","Sungai Jawi"]
},
],
"Land_use": [
{
"shp_name": "Land Use 2019",
"color_prop": ["#000000"],
"shp_prop": ["Grassland"]
},
]
}
If anyone could assist me with this issue, I would greatly appreciate it. Thank you.