
- 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
How to convert square bracket object keys into nested object in JavaScript?
We know that there are two ways we can access nested keys within an Object in JavaScript.
For instance, take this object −
const obj = { object: { foo: { bar: { ya: 100 } } } };
If we needed to access or update the nested property 'ya', we can access it like −
Way 1 −
obj['object']['foo']['bar']['ya']
orWay 2 −
obj.object.foo.bar.ya
Both these ways lead us to the same destination.
We are required to write a JavaScript function that takes in the path to nested key as a string as depicted by Way 1 and converts it to the notation depicted by Way 2
Example
The code for this will be −
const obj = { "object[foo][bar][ya]": 100 }; const constructDotNotation = obj => { const keys = Object.keys(obj)[0].split('[').map(el => { return el.replace(']', ''); }); let res = {}; keys.reverse().forEach(key => { if (Object.keys(res).length === 0){ res[key] = obj[Object.keys(obj)[0]]; }else{ const temp = {}; temp[key] = res; res = temp; }; }); return res; }; console.log(JSON.stringify(constructDotNotation(obj), undefined, 4));
Output
And the output in the console will be −
{ "object": { "foo": { "bar": { "ya": 100 } } } }
- Related Articles
- Recursively list nested object keys JavaScript
- Filter nested object by keys using JavaScript
- Changing value of nested object keys in JavaScript
- How to convert an object into an array in JavaScript?
- How to convert a JSON string into a JavaScript object?
- Convert JS array into an object - JavaScript
- How to convert a plain object into ES6 Map using JavaScript?
- Strip quotes with JavaScript to convert into JSON object?
- How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
- Print JSON nested object in JavaScript?
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- How to convert a date object's content into json in JavaScript?
- How to modify properties of a nested object in JavaScript?
- How to set dynamic property keys to an object in JavaScript?
- How to convert array to object in JavaScript

Advertisements