Manipulate Object to group based on Array Object List in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Suppose, we have an array of objects that contains information about some cars like this −

const arr = [
   {
      "group":[],
      "name": "All Makes",
      "code": ""
   },
   {
      "group":["Group A"],
      "name": "BMW",
      "code": "X821"
   },
   {
      "group":["Group B"],
      "name": "Audi",
      "code": "B216"
   },
   {
      "group":["Group B"],
      "name": "Ford",
      "code": "P385"
   },
   {
      "group":["Group B", "Group C"],
      "name": "Mercedes",
      "code": "H801"
   },
   {
      "group":["Group C"],
      "name": "Honda",
      "code": "C213"
   }
];

We are required to write a JavaScript function that takes in one such array of objects. The function should group the objects of this array based on the group property of each object. If the group property contains more than one element then that object should appear in both the groups.

Therefore.], for the above array, the output should look like −

const output = [
   {
      "group": "Group A",
   "cars": [
      {
         name: "BMW",
         code: "X821"
      }
   ]
},
{
   "group": "Group B",
   "cars": [
      {
         name: "Audi",
         code: "B216"
   },
   {
      name: "Ford",
      code: "P385"
   },
   {
      name: "Mercedes",
      code: "H801"
   }
   ]
},
{
   "group": "Group C",
   "cars": [
      {
         name: "Mercedes",
         code: "H801"
      },
      {
         name: "Honda",
         code: "C213"
      }
   ]
   }
];

Example

The code for this will be −

const arr = [
   {
      "group":[],
      "name": "All Makes",
      "code": ""
   },
   {
      "group":["Group A"],
      "name": "BMW",
      "code": "X821"
   },
   {
      "group":["Group B"],
      "name": "Audi",
      "code": "B216"
   },
   {
      "group":["Group B"],
      "name": "Ford",
      "code": "P385"
   },
   {
      "group":["Group B", "Group C"],
      "name": "Mercedes",
      "code": "H801"
   },
   {
      "group":["Group C"],
      "name": "Honda",
      "code": "C213"
   }
];
const groupTogether = (arr = []) => {
   let res = []
   res = Object.entries(arr.reduce((acc, { group, ...r }) => {
      group.forEach(key => acc[key] = (acc[key] ||
      []).concat({...r}));
      return acc;
   }, {}))
   return res.map(([group, arr]) => ({ group, arr }));
};
console.log(JSON.stringify(groupTogether(arr), undefined, 4));

Output

And the output in the console will be −

[
   {
      "group": "Group A",
      "arr": [
         {
            "name": "BMW",
            "code": "X821"
         }
      ]
   },
 {
   "group": "Group B",
   "arr": [
      {
         "name": "Audi",
         "code": "B216"
      },
      {
         "name": "Ford",
         "code": "P385"
      },
      {
         "name": "Mercedes",
         "code": "H801"
      }
   ]
},
{
   "group": "Group C",
   "arr": [
      {
         "name": "Mercedes",
         "code": "H801"
      },
      {
         "name": "Honda",
         "code": "C213"
      }
   ]
   }
]
raja
Published on 23-Nov-2020 07:38:13
Advertisements