I have a problem in my javascript code, i need to create an object with variables and those variables being objects too. Explanation:
I need this in my javascript code (similar to a Json structure):
var myObj = {
variableOne: {
variableOneA: 'someValue',
variableOneB: 'someValue'
}
variableTwo: {
variableTwoA: 'someValue',
variableTwoB: 'someValue'
}
variableThree: {
variableThreeA: 'someValue',
variableThreeB: 'someValue'
}
}
Now, my problem with this is that in Js i cannot do a 'push' method to an object and i can only add one level of variables to my object doing this:
myObj.variableOne = 'someValue';
Can anyone help me please? i believe the resolution could be easy but i am new to Js.
myObj.variableOne.variableOneA = 'someValue
' - you mean like this? You can access each deeper level with the.
character. Alternatively, if it's a list, why not use an array instead?myObj.variableOne = ['someValue', 'someValue']
, etc.myObj.variableOne = {variableOneA:"somevalue"}
. You can make it as complex as you want. Actually our first snippet is valid js that would give you an object with the wanted structuresomeValue
in thevariableOneA
? If yes, then how would it know the name propertyvariableOneA
if you're not specifying it anywhere?