
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Regroup JSON array in JavaScript
Suppose, we have a JSON array of objects like this −
const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", "month_7": 5, }, { "id": "51295", "month_1": 1, }, { "id": "51295", "month_10": 1, }, { "id": "55468", "month_11": 1, } ];
Here, we can see that the same "id" property is being repeated in some objects. We are required to write a JavaScript function that takes in one such array that contains all the key/value pairs for a specific "id" property grouped in one single object.
Example
The code for this will be −
const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", "month_7": 5, }, { "id": "51295", "month_1": 1, }, { "id": "51295", "month_10": 1, }, { "id": "55468", "month_11": 1, } ]; const groupById = (arr = []) => { const map = {}; const res = []; arr.forEach(el => { if(map.hasOwnProperty(el['id'])){ const index = map[el['id']] - 1; const key = Object.keys(el)[1]; res[index][key] = el[key]; } else{ map[el['id']] = res.push(el); } }) return res; }; console.log(groupById(arr));
Output
And the output in the console will be −
[ { id: '03868185', month_10: 6 }, { id: '03870584', month_6: 2, month_7: 5 }, { id: '51295', month_1: 1, month_10: 1 }, { id: '55468', month_11: 1 } ]
- Related Articles
- Convert JSON array into normal json in JavaScript
- Formatting dynamic json array JavaScript
- Build tree array from JSON in JavaScript
- How to iterate json array – JavaScript?
- Merge JSON array date based JavaScript
- Create array from JSON object JavaScript
- JavaScript Convert an array to JSON
- From JSON object to an array in JavaScript
- JavaScript creating an array from JSON data?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to turn a JSON object into a JavaScript array in JavaScript ?
- How to read data from JSON array using JavaScript?
- Search by id and remove object from JSON array in JavaScript
- How to modify an array value of a JSON object in JavaScript?
- JavaScript JSON Arrays

Advertisements