I have an array and i want to filter this array by Country and Service
i did the filter by Country but i want also do the same thing by service
this 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",
},
];
I was able to do that by this code
let objectData = Data.reduce(function (acc,cur) {
if (!acc[cur.Country])
acc[cur.Country] = { data : []};
acc[cur.Country].data.push(cur)
return acc;
},
{} );
the code above allowed me to filter only by country and it's work but i want to do this same thing by country and service BOTH and i want the result like this :
[
{
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"}]},
]
},
]