0

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"}]},

        ] 
       },
       
   ]

brabus
  • 25
  • 7
  • The code is the exact same, you just need to do the `.reduce(...)` on each country's `Services` property after they have been grouped. It's easiest with two loops. – Klaycon Aug 25 '20 at 21:05
  • [Group array items using object](https://stackoverflow.com/q/31688459/215552) will get you 90% of the way there... The idea of breaking out properties into individual objects in nested arrays is ... unique. – Heretic Monkey Aug 25 '20 at 21:07
  • Also, isn't there any way you can just make these objects instead of arrays? I don't know why every single place where dictionaries are *perfect* people always, always use arrays instead. – Klaycon Aug 25 '20 at 21:09

1 Answers1

0

This is a 2 step algorithmus first creating the data and second bring it in the right form.
Use Array#reduce tom collect the data. If there exists in the result.object any key for this country create a property with it and set the country and an empty Service-array.
Look if there exists a service-property for this service. If not create such an property with it's name and an empty data-array. Now pus (allways) to this array the details.
Get from this created result-object with Object.#values an array.
At last the Services are an object too so use Array#forEach and Object.value again to convert all of them to arrays.

let data = [
   {
      "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",
   }
];

let res = Object.values(data.reduce((acc, cur) => {
    if (!acc[cur.Country]) {
        acc[cur.Country] = {Country: cur.Country, Services: []};
    }
    if (!acc[cur.Country].Services[cur.Service]) {
        acc[cur.Country].Services[cur.Service] = {name: cur.Service, data: []};
    }
    acc[cur.Country].Services[cur.Service].data.push({details: cur.details});
    return acc;
}, {}));

res.forEach(obj => {
   obj.Services = Object.values(obj.Services);
});

console.log(res);
Sascha
  • 4,576
  • 3
  • 13
  • 34