
- 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
Grouping nested array in JavaScript
Suppose, we have an array of values like this −
const arr = [ { value1:[1,2], value2:[{type:'A'}, {type:'B'}] }, { value1:[3,5], value2:[{type:'B'}, {type:'B'}] } ];
We are required to write a JavaScript function that takes in one such array. Our function should then prepare an array where the data is grouped according to the "type" property of the objects.
Therefore, for the above array, the output should look like −
const output = [ {type:'A', value: [1,2]}, {type:'B', value: [3,5]} ];
Example
The code for this will be −
const arr = [ { value1:[1,2], value2:[{type:'A'}, {type:'B'}] }, { value1:[3,5], value2:[{type:'B'}, {type:'B'}] } ]; const groupValues = (arr = []) => { const res = []; arr.forEach((el, ind) => { const thisObj = this; el.value2.forEach(element => { if (!thisObj[element.type]) { thisObj[element.type] = { type: element.type, value: [] } res.push(thisObj[element.type]); }; if (!thisObj[ind + '|' + element.type]) { thisObj[element.type].value = thisObj[element.type].value.concat(el.value1); thisObj[ind + '|' + element.type] = true; }; }); }, {}) return res; }; console.log(groupValues(arr));
Output
And the output in the console will be −
[ { type: 'A', value: [ 1, 2 ] }, { type: 'B', value: [ 1, 2, 3, 5 ] } ]
- Related Articles
- Grouping array nested value while comparing 2 objects - JavaScript
- Grouping array values in JavaScript
- Complicated array grouping JavaScript
- Simplifying nested array JavaScript
- Grouping and sorting 2-D array in JavaScript
- Join in nested array in JavaScript
- Recursion - Sum Nested Array in JavaScript
- Grouping array of array on the basis of elements in JavaScript
- Convert nested array to string - JavaScript
- Transform nested array into normal array with JavaScript?
- Finding the maximum in a nested array - JavaScript
- JavaScript - summing numbers from strings nested in array
- Weight sum of a nested array in JavaScript
- Array grouping on the basis of children object’s property in JavaScript
- Accessing and returning nested array value - JavaScript?

Advertisements