
- 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
Recursive string parsing into object - JavaScript
We are required to write a JavaScript function that takes in an array of strings and returns an object corresponding to the strings.
For example −
If the array is −
const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ];
Then the output should be −
const output = { "country": [ {"UK" : {"level" : ["1", "2", "3"]}}, {"US" : {"level" : ["1","2"]}} ] }
Conditions
Strings stored in the str array will not be sorted and the code should be robust against that.
Strings will follow the x.y.x.y... pattern, where x will be unique for that array and y can change. In my example country and level will always be the same as they represent the x pos.
This requires recursive approach as the strings stored in the str array, can be of any length. The longer the string the deeper nesting.
Example
Following is the code −
const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ]; const stringToObject = arr => { const obj = {}; arr.forEach(str => { let curr = obj; let splitted = str.split('.'); let last = splitted.pop(); let beforeLast = splitted.pop(); splitted.forEach( sub => { if(!curr.hasOwnProperty(sub)){ curr[sub] = {}; }; curr = curr[sub]; }); if(!curr[beforeLast]){ curr[beforeLast] = []; }; curr[beforeLast].push(last); }); return obj; }; console.log(JSON.stringify(stringToObject(arr), undefined, 4));
Output
This will produce the following output on console −
{ "country": { "UK": { "level": [ "1", "2", "3" ] }, "US": { "level": [ "1" ] } } }
- Related Articles
- How to convert a JSON string into a JavaScript object?
- How to return an object by parsing http cookie header in JavaScript?
- Parsing array of objects inside an object using maps or forEach using JavaScript?
- JavaScript Quicksort recursive
- Convert dictionary object into string in Python
- Split string into groups - JavaScript
- Splitting string into groups – JavaScript
- Reverse a String (Recursive) C++
- Segregating a string into substrings - JavaScript
- Split string into equal parts JavaScript
- Convert JS array into an object - JavaScript
- Form Object from string in JavaScript
- Parsing a comma-delimited std::string in C++
- Recursive multiplication in array - JavaScript
- Recursive Staircase problem in JavaScript

Advertisements