
- 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
Recursively flat an object JavaScript
We are required to write a function that does the following transformation −
If the input object is −
const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 };
Then the output of the function should be −
const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 }
We basically have to write a function that flattens a nested object. Will do this via recursion and the code for doing this will be −
Example
const obj = { a: 1, b: {x: {y: 1, z: 2}}, c: 3 }; const obj1 = { a: 0, b: 0, c: 0 }; const object = { a: 0, b: { x: { y: 1, z: 2 } }, c: 3 }; const stringifyKeys = (obj, str = '', fresh = {}) => { const keys = Object.keys(obj); for(let i = 0; i < keys.length; i++){ if(typeof obj[keys[i]] === "object" && !Array.isArray(obj[keys[i]]) && obj[keys[i]]){ stringifyKeys(obj[keys[i]], str+`${keys[i]}.`, fresh); }else{ fresh[str+keys[i]] = obj[keys[i]]; }; } return fresh; }; console.log(stringifyKeys(obj)); console.log(stringifyKeys(object)); console.log(stringifyKeys(obj1));
Output
The output in the console will be −
{ a: 1, 'b.x.y': 1, 'b.x.z': 2, c: 3 } { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } { a: 0, b: 0, c: 0 }
- Related Articles
- Flat a JavaScript array of objects into an object
- Recursively list nested object keys JavaScript
- What is the simplest solution to flat a JavaScript array of objects into an object?
- How to check if every property on object is the same recursively in JavaScript?
- Recursively loop through an array and return number of items with JavaScript?
- Flat array of objects to tree in JavaScript
- Build tree array from flat array in JavaScript
- Recursively adding digits of a number in JavaScript
- Get all substrings of a string in JavaScript recursively
- Reverse mapping an object in JavaScript
- Normalize numbers in an object - JavaScript
- Filter an object based on an array JavaScript
- Java Program to Recursively Linearly Search an Element in an Array
- How to access an object through another object in JavaScript?
- Remove number properties from an object JavaScript

Advertisements