0

How do you create a deep nested property using a string?
I know you can use bracket notation, but if you're trying to create a nested property. i.e.

Obj{
   l1:{
      l2:{}
   }
} 

It doesn't work as I want it to. I get an obj property of "levelTwo.two" as opposed to an actual property of "levelTwo",followed by another nested property of "two". See example below:

var obj = {};
var value= 'test';
var str = "levelOne";
var str2 ="levelTwo.two";
var strX = "levelX";

obj[str] = value
console.log(obj);

obj[str2]= value;
console.log(obj);
Cyph
  • 623
  • 1
  • 7
  • 25

0 Answers0