I am working with an array and need to filter it by both Country and Service.
So far, I have successfully filtered the array by Country. Now, I want to achieve the same filtering process based on the Service as well.
Here is a snippet of the array:
[
{
"Country":"CHINA",
"details":"V2020",
"Service":"BUSINESS",
},
{
"Country":"CHINA",
"details":"V3030",
"Service":"BUSINESS",
},
{
"Country":"USA",
"details":"Bus-Trip",
"Service":"BUSINESS",
},
{
"Country":"USA",
"details":"Comm-Trip",
"Service":"COMMUNICATION",
},
];
Currently, I have used the following code to filter the data by country:
let objectData = Data.reduce(function (acc,cur) {
if (!acc[cur.Country])
acc[cur.Country] = { data : []};
acc[cur.Country].data.push(cur)
return acc;
},
{} );
Although this code effectively filters the array by country, my goal now is to filter by both country and service SIMULTANEOUSLY. The desired result should be structured like below:
[
{
Country :"CHINA",
Services : [
{"name":"BUSINESS", data : [{"details":"V2020"},{"details":"V3030"}]},
]
},
{
Country :"USA" ,
Services : [
{"name":"BUSINESS", data : [{"details":"Bus-Trip20"}]},
{"name":"COMMUNICATION", data : [{"details":"Comm-Trip30"}]},
]
},
]