How can I convert a string to a nested object in JavaScript?
Richard W
richard w profile pic

To convert a string to a nested object in JavaScript, you can use a combination of string manipulation and object assignment techniques. Here's a step-by-step guide on how to achieve this: 1. Split the string into keys: - Determine the delimiter that separates the keys in the string. For example, if the string is in dot notation (e.g., "parent.child.property"), the delimiter is a dot. - Use thesplit() method to split the string into an array of keys.

1
2
3
4
5
6
7
8
9
10
11
12

   function stringToNestedObject(str, delimiter) {
     const keys = str.split(delimiter);
     return keys;
   }

   const string = "parent.child.property";
   const delimiter = ".";
   const keys = stringToNestedObject(string, delimiter);
   console.log(keys);
   // Output: ["parent", "child", "property"]
   

2. Create the nested object: - Iterate through the array of keys and create a nested object structure. - Start with an empty object and assign each key as a new object property until you reach the last key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

   function stringToNestedObject(str, delimiter) {
     const keys = str.split(delimiter);
     const nestedObj = {};

     let currentObj = nestedObj;
     for (let i = 0; i < keys.length - 1; i++) {
       const key = keys[i];
       currentObj[key] = {};
       currentObj = currentObj[key];
     }

     const lastKey = keys[keys.length - 1];
     currentObj[lastKey] = null;

     return nestedObj;
   }

   const string = "parent.child.property";
   const delimiter = ".";
   const nestedObj = stringToNestedObject(string, delimiter);
   console.log(nestedObj);
   // Output: { parent: { child: { property: null } } }
   

In this example, the functionstringToNestedObject() iterates through the keys and assigns them as properties of nested objects. The last key is assigned a value ofnull in this example, but you can modify it based on your requirements. 3. Customizing property values: - You can modify the code to assign custom values to the properties instead ofnull. For example, if you have a specific value associated with the string, you can assign that value to the final property.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

   function stringToNestedObject(str, delimiter, value) {
     const keys = str.split(delimiter);
     const nestedObj = {};

     let currentObj = nestedObj;
     for (let i = 0; i < keys.length - 1; i++) {
       const key = keys[i];
       currentObj[key] = {};
       currentObj = currentObj[key];
     }

     const lastKey = keys[keys.length - 1];
     currentObj[lastKey] = value;

     return nestedObj;
   }

   const string = "parent.child.property";
   const delimiter = ".";
   const value = 42;
   const nestedObj = stringToNestedObject(string, delimiter, value);
   console.log(nestedObj);
   // Output: { parent: { child: { property: 42 } } }
   

In this modified example, thevalue parameter is assigned to the final property of the nested object. By following these steps, you can convert a string into a nested object. Keep in mind that this implementation assumes a specific key delimiter (e.g., dot notation) and assigns values to the final property. You can customize the implementation based on your specific requirements, such as handling different delimiters or assigning different values to properties.