

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Manipulate Object to group based on Array Object List in JavaScript
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" } ] } ]
- Related Questions & Answers
- Filter an object based on an array JavaScript
- Group by JavaScript Array Object
- Sort object array based on another array of keys - JavaScript
- How to manipulate JavaScript's Date object?
- How to filter object array based on attributes in jQuery?
- JSON group object in JavaScript
- Filter the properties of an object based on an array and get the filtered object JavaScript
- Create an object based on 2 others in JavaScript
- JavaScript - Sort key value pair object based on value?
- Object to array - JavaScript
- map() array of object titles into a new array based on other property value JavaScript
- Iterate through Object keys and manipulate the key values in JavaScript
- Merge and group object properties in JavaScript
- Display resultant array based on the object’s order determined by the first array in JavaScript?
- Transforming array to object JavaScript
Advertisements