var objeto = {
sub1_A: {},
sub1_B: {},
sub1_C: {}
}
I have no problems if I do the following:
objeto.sub1_A.sub2 = "Prueba";
But if I go down one more level directly:
objeto.sub1_A.sub2.sub3 = "Prueba";
If I try to set the above it says "Uncaught TypeError: Cannot set property 'recodes' of undefined". I understand that it is because you can set/define a method or property of an already existing object but not of one that doesn't.
Isn't there a way to drop as many levels as you want by creating said "path"?
If you wanted to create something like object.sub1.sub2.sub3.sub4.sub5="SetealoYa!" Am I forced to go step by step? I am convinced that there must be another method.
Thank you very much in advance!
You can do it with
objeto.sub1_A.sub2 = "Prueba";
since sub1_A is an object, this object you defined in your parent objectWhen you define
sub2
, you are defining that itsub1_A
has a property of type called stringsub2
whose value is test.When doing it, it gives
objeto.sub1_A.sub2.sub3 = "Prueba";
you an errorUncaught TypeError: Cannot set property 'recodes' of undefined
because there is no property of sub1_A that is sub2.This property would have to be an object in order for it to have another property. So you could do the following.
If you wanted to add one more level, you would have to convert sub3 to an object, since you can't add properties to a string.