
- 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
Group objects inside the nested array JavaScript
Let’s say e have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same pair of n keys where n is the size of the sub array.
Our job is to prepare an object with key as key of objects and value being an array that contains all the values for that particular key.
Here is our sample parent array −
const parentArray = [[ { key: 123, value: 'India' }, { key: 124, value: 'USA' }, { key: 125, value: 'Japan' }, { key: 126, value: 'Denmark' }, { key: 127, value: 'Austria' }, ], [ { key: 124, value: 'Kenya' }, { key: 126, value: 'UK' }, { key: 123, value: 'Germany' }, { key: 127, value: 'Spain' }, { key: 125, value: 'Portugal' }, ]];
We will iterate over the parent array and then all of the sub arrays one by one and if we find a matching key, we push it into the value array otherwise we create a new value array.
The full code for this will be −
Example
const parentArray = [[ { key: 123, value: 'India' }, { key: 124, value: 'USA' }, { key: 125, value: 'Japan' }, { key: 126, value: 'Denmark' }, { key: 127, value: 'Austria' }, ], [ { key: 124, value: 'Kenya' }, { key: 126, value: 'UK' }, { key: 123, value: 'Germany' }, { key: 127, value: 'Spain' }, { key: 125, value: 'Portugal' }, ]]; const map = {}; parentArray.forEach(arr => { arr.forEach(obj => { const { key, value } = obj; if(map[key]){ map[key].push(value); }else{ map[key] = [value] } }) }); console.log(map);
Output
The output in the console will be −
{ '123': [ 'India', 'Germany' ], '124': [ 'USA', 'Kenya' ], '125': [ 'Japan', 'Portugal' ], '126': [ 'Denmark', 'UK' ], '127': [ 'Austria', 'Spain' ] }
- Related Articles
- Grouping array nested value while comparing 2 objects - JavaScript
- MongoDB Increment value inside nested array?
- How to convert nested array pairs to objects in an array in JavaScript ?
- JavaScript array.includes inside nested array returning false where as searched name is in array
- Increment MongoDB value inside a nested array
- How to group array of objects by Id in JavaScript?
- How to sort an array of objects based on the length of a nested array in JavaScript
- How to group an array of objects by key in JavaScript
- ES6 Default Parameters in nested objects – JavaScript
- Accessing nested JavaScript objects with string key
- Sort nested array containing objects ascending and descending according to date in JavaScript
- Simplifying nested array JavaScript
- Group objects by property in JavaScript
- Parsing array of objects inside an object using maps or forEach using JavaScript?
- How to access nested json objects in JavaScript?

Advertisements