How to Regroup JSON array in JavaScript
Last Updated :
18 Apr, 2024
Regrouping JSON arrays in JavaScript is crucial for organizing data efficiently, simplifying data management tasks, and ensuring better data integration across systems. Here, we will regroup JSON arrays in JavaScript in two methods to achieve this using the reduce method and an iterative approach.
Using Iterative Method
In this approach, The function begins by initializing an empty object called "group" to store regrouped data. It iterates over each item in the jsonArray using forEach(). Each item's category property is checked, and if a corresponding key doesn't exist in the "group" object, one is created with an empty array. Items are then pushed into their respective category arrays. Finally, the function returns the "group" object with regrouped data based on categories.
Example: In this example regrouping JSON array in JavaScript using a loop-based method.
JavaScript
function regroupByCategory(jsonArray) {
const grouped = {};
// Iterate over the array
jsonArray.forEach(item => {
if (!grouped[item.category]) {
grouped[item.category] = [];
}
// Push the item into the array
grouped[item.category].push(item);
});
return grouped;
}
const jsonArray = [
{ id: 1, category: "A" },
{ id: 2, category: "B" },
{ id: 3, category: "A" },
{ id: 4, category: "C" },
{ id: 5, category: "B" },
{ id: 6, category: "A" }
];
const regrouped = regroupByCategory(jsonArray);
console.log(regrouped);
Output{
A: [
{ id: 1, category: 'A' },
{ id: 3, category: 'A' },
{ id: 6, category: 'A' }
],
B: [ { id: 2, category: 'B' }, { id: 5, category: 'B' } ],
C: [ { id: 4, category: 'C' } ]
}
Using Array.reduce()
In this approach, The reduce() method initializes an empty object as the accumulator. It iterates over each item in the jsonArray, grouping them by their category property. If a category key doesn't exist, it creates one with an empty array. It then pushes items into their respective category arrays. Finally, it returns the grouped object containing the regrouped data.
Example: The below example shows a regrouped JSON array in JavaScript Using Array.reduce().
JavaScript
function regroupByCategory(jsonArray) {
return jsonArray.reduce((grouped, item) => {
if (!grouped[item.category]) {
grouped[item.category] = [];
}
grouped[item.category].push(item);
return grouped;
}, {});
}
const jsonArray = [
{ id: 1, category: "A" },
{ id: 2, category: "B" },
{ id: 3, category: "A" },
{ id: 4, category: "C" },
{ id: 5, category: "B" },
{ id: 6, category: "A" }
];
const regrouped = regroupByCategory(jsonArray);
console.log(regrouped);
Output{
A: [
{ id: 1, category: 'A' },
{ id: 3, category: 'A' },
{ id: 6, category: 'A' }
],
B: [ { id: 2, category: 'B' }, { id: 5, category: 'B' } ],
C: [ { id: 4, category: 'C' } ]
}
Similar Reads
How to Remove Duplicates in JSON Array JavaScript ? In JavaScript, removing duplicates from a JSON array is important for data consistency and efficient processing. We will explore three different approaches to remove duplicates in JSON array JavaScript. Use the methods below to remove Duplicates in JSON Array JavaScript. Table of Content Using SetUs
3 min read
How to Parse JSON in JavaScript ? Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipula
2 min read
How to Serialize JSON in JavaScript ? JSON (JavaScript Object Notation) serialization is a fundamental concept in JavaScript, allowing the conversion of JavaScript objects into strings that can be easily transmitted over a network or stored in a file. We will explore how to serialize JSON in JavaScript using JSON.stringify(). Approach I
1 min read
How to Convert JSON to ArrayBuffer in JavaScript? An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer,
4 min read
How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read