Grouping Nested Array in JavaScript
Last Updated :
02 May, 2024
Grouping nested arrays in JavaScript involves arranging arrays within arrays based on a common key or property.
There are different approaches to group nested arrays in JavaScript which are as follows:
Using the reduce() method
This approach uses the reduce() method to group arrays within a nested array based on a shared key. It creates a new object where each key corresponds to a unique property value and the value is an array of objects with that property value.
Example: To demonstrate grouping a nested array in JavaScript using the reduce() method in JavaScript.
JavaScript
const students = [
{ name: 'Saurabh', div: 'A' },
{ name: 'Gaurav', div: 'C' },
{ name: 'Prasad', div: 'B' },
{ name: 'Rohit', div: 'C' },
{ name: 'Pratik', div: 'A' }
];
const groupedStudents = students
.reduce((acc, curr) => {
const key = curr.div;
(acc[key] = acc[key] || [])
.push(curr);
return acc;
}, {});
console.log(groupedStudents);
Output{
A: [ { name: 'Saurabh', div: 'A' }, { name: 'Pratik', div: 'A' } ],
C: [ { name: 'Gaurav', div: 'C' }, { name: 'Rohit', div: 'C' } ],
B: [ { name: 'Prasad', div: 'B' } ]
}
Using the forEach() method with an object as a map
In this approach we iterate over the nested array using the forEach() method and use an object as a map to group arrays based on a common key. For each element in the nested array, we check if the key already exists in the map object. If it does we push the element to the corresponding array. otherwise we create a new array for that key.
Example: To demonsrtate grouping nested array in JavaScript using the usesforEach() method with an object as a map to group nested arrays.
JavaScript
const students = [
{ name: 'Saurabh', div: 'A' },
{ name: 'Gaurav', div: 'C' },
{ name: 'Prasad', div: 'B' },
{ name: 'Rohit', div: 'C' },
{ name: 'Pratik', div: 'A' }
];
const groupedStudents = {};
students.forEach((student) => {
const key = student.div;
groupedStudents[key] = groupedStudents[key] || [];
groupedStudents[key].push(student);
});
console.log(groupedStudents);
Output{
A: [ { name: 'Saurabh', div: 'A' }, { name: 'Pratik', div: 'A' } ],
C: [ { name: 'Gaurav', div: 'C' }, { name: 'Rohit', div: 'C' } ],
B: [ { name: 'Prasad', div: 'B' } ]
}
Similar Reads
JavaScript Array from() Method The Array.from() method is used to create a new array from any iterables like array, objects, and strings.JavaScriptconst a = Array.from("GeeksforGeeks"); console.log(a);Output[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ] SyntaxArray.from(object, mapFunction, thisValue);Paramet
2 min read
JavaScript - Group Objects by Property into Arrays Here are the various approaches to grouping objects by property into arrays1. Using a for LoopThe for loop is the most basic and widely used approach.JavaScriptconst a = [ { cat: 'fruit', name: 'apple' }, { cat: 'veg', name: 'carrot' }, { cat: 'fruit', name: 'banana' } ]; const grouped = []; const g
3 min read
Reverse an Array in Groups of Given Size in JavaScript We are going to reverse a sub-array of a given size. We will be provided with an array and a random size for the sub-array to be reversed. There exist different variations of this problem. These are the following variations of reversing the array in groups of a given size:Table of ContentReverse the
9 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
Split an Array into Chunks in JavaScript Here are different methods to split an array into chunks in JavaScript.1. Using slice() MethodThe array slice() method returns a new array containing the selected elements. This method selects the elements starting from the given start argument and ends at, but excluding the given end argument. Synt
3 min read