
- 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
Flat a JavaScript array of objects into an object
To flat a JavaScript array of objects into an object, we created a function that takes array of object as only argument. It returns a flattened object with key append by its index. The time complexity is O(mn) where n is the size of array and m is the number of properties in each object. However, its space complexity is O(n) where n is the size of actual array.
Example
//code to flatten array of objects into an object //example array of objects const notes = [{ title: 'Hello world', id: 1 }, { title: 'Grab a coffee', id: 2 }, { title: 'Start coding', id: 3 }, { title: 'Have lunch', id: 4 }, { title: 'Have dinner', id: 5 }, { title: 'Go to bed', id: 6 }, ]; const returnFlattenObject = (arr) => { const flatObject = {}; for(let i=0; i<arr.length; i++){ for(const property in arr[i]){ flatObject[`${property}_${i}`] = arr[i][property]; } }; return flatObject; } console.log(returnFlattenObject(notes));
Output
Following is the output in console −
[object Object] { id_0: 1, id_1: 2, id_2: 3, id_3: 4, id_4: 5, id_5: 6, title_0: "Hello world", title_1: "Grab a coffee", title_2: "Start coding", title_3: "Have lunch", title_4: "Have dinner", title_5: "Go to bed" }
- Related Articles
- What is the simplest solution to flat a JavaScript array of objects into an object?
- Splitting an object into an array of objects in JavaScript
- Convert an array of objects into plain object in JavaScript
- JavaScript Converting array of objects into object of arrays
- Flat array of objects to tree in JavaScript
- Recursively flat an object JavaScript
- Converting array of objects to an object of objects in JavaScript
- How to merge objects into a single object array with JavaScript?
- Converting array of objects to an object in JavaScript
- Convert JS array into an object - JavaScript
- Convert array of objects to an object of arrays in JavaScript
- Converting array of arrays into an object in JavaScript
- How to transform object of objects to object of array of objects with JavaScript?
- How to combine two arrays into an array of objects in JavaScript?
- Convert 2d tabular data entries into an array of objects in JavaScript

Advertisements