How to Regroup JSON array in JavaScript Last Updated : 18 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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. Table of Content Using Iterative MethodUsing Array.reduce()Using Iterative MethodIn 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' } ] } Comment More infoAdvertise with us Next Article How to Regroup JSON array in JavaScript G gfgking Follow Improve Article Tags : JavaScript Web Technologies JSON 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 How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A 2 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to Convert CSV to JSON in JavaScript ? In this article, we will explain different ways to change Comma-Separated Values (CSV) data into JavaScript Object Notation (JSON) format, step-by-step. We'll break down each method with clear explanations and examples. There are several approaches available in JavaScript to convert CSV to JSON in J 3 min read How to view array of a structure in JavaScript ? The Javascript arrays are heterogeneous. The structure of the array is the same which is enclosed between two square brackets [ ], and the string should be enclosed between either "double quotes" or 'single quotes'. You can get the structure of by using JSON.stringify() or not, here you will see the 3 min read Like