
- 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
JavaScript Separate objects based on properties
Suppose, we have an object like this −
const obj = { 0: { "time": 1, "day": 1, }, 1: { "time": 2, "day": 1, }, 2: { "time": 3, "day": 1, }, 3: { "time": 1, "day": 2, }, 4: { "time": 2, "day": 2, }, 5: { "time": 3, "day": 2, } };
We are required to write a JavaScript function that takes one such object and groups all the key value pairs in separate sub objects that have the same value for the day key.
Output
The output for the above object should be −
const output = { '1': { '1': { time: 1, day: 1 }, '2': { time: 2, day: 1 }, '3': { time: 3, day: 1 } }, '2': { '1': { time: 1, day: 2 }, '2': { time: 2, day: 2 }, '3': { time: 3, day: 2 } } }
Example
The code for this will be −
const obj = { 0: { "time": 1, "day": 1, }, 1: { "time": 2, "day": 1, }, 2: { "time": 3, "day": 1, }, 3: { "time": 1, "day": 2, }, 4: { "time": 2, "day": 2, }, 5: { "time": 3, "day": 2, } }; const groupObject = obj => { let res = {}; res = Object.values(obj).reduce((acc, val) => { if(acc[val['day']] === undefined){ acc[val['day']] ={}; }; acc[val['day']][val['time']] = val; return acc; },{}); return res; }; console.log(groupObject(obj));
Output
The output in the console −
{ '1': { '1': { time: 1, day: 1 }, '2': { time: 2, day: 1 }, '3': { time: 3, day: 1 } }, '2': { '1': { time: 1, day: 2 }, '2': { time: 2, day: 2 }, '3': { time: 3, day: 2 } } }
- Related Articles
- Grouping objects based on key property in JavaScript
- Creating an array of objects based on another array of objects JavaScript
- Filter an array containing objects based on another array containing objects in JavaScript
- Sort array based on presence of fields in objects JavaScript
- How to group objects based on a value in JavaScript?
- Split keys and values into separate objects - JavaScript
- JavaScript Sum of two objects with same properties
- How to merge properties of two JavaScript Objects dynamically?
- Sort Array of objects by two properties in JavaScript
- Filter the properties of an object based on an array and get the filtered object JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- Sort an array of objects by multiple properties in JavaScript
- How to create Python objects based on an XML file?
- Filter array of objects whose properties contains a value in JavaScript
- How to access properties of an array of objects in JavaScript?

Advertisements