-1

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.

3
  • 2
    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. Commented Nov 21, 2019 at 21:29
  • You can assign a new object to a property: 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 structure Commented Nov 21, 2019 at 21:33
  • What exactly do you expect "push to the object" to do, automatically create that object and put the someValue in the variableOneA? If yes, then how would it know the name property variableOneA if you're not specifying it anywhere?
    – Bergi
    Commented Nov 21, 2019 at 22:29

2 Answers 2

3

There are different ways to access your object in Javascript.

var myObj = {
   variableOne: {
             variableOneA: 'oneA',
             variableOneB: 'oneB'
         }
   variableTwo: {
             variableTwoA: 'twoA',
             variableTwoB: 'twoB
         }
   variableThree: {
             variableThreeA: 'threeA',
             variableThreeB: 'threeB'
         }
 }

You can use "dot" to access a particular level of your object.

const valueVariableOneA = myObj.variableOne.variableOneA
console.log(valueVariableOneA) // output => "oneA"

You can use the square brackets in replacement of dot. Square brackets are usefull when you want to create an object's key with dash (eg: "cool-key")

const valueVariableThreeB = myObj['variableThree']['variableThreeB']
console.log(valueVariableThreeB) // output => "threeB"

You can also use the destructuration to access particular value

// Get value of variableTwoA key

const { variableTwoA } = myObj.variableTwo // first way
const { variableTwo : { variableTwoA } } = myObj // second way

console.log(variableTwoA) // output => "twoA"

Now to add a key to a nested object you can use either dot or square brackets method. Here's how to add key on the first level.

myObj.variableFour = { variableFourA: 'fourA', variableFourB: 'fourB' }
myObj['variableFour'] = { variableFourA: 'fourA', variableFourB: 'fourB' }

// add key on nested object

myObj.variableOne.variableOneC = 'oneC'
myObj['variableOne']['variableOneC'] = 'oneC'

// you can mix both
myObj['variableOne'].variableOneC = 'oneC'
myObj.variableOne['variableOneC'] = 'oneC'
1
  • Thanks a lot, thats it!! Commented Nov 21, 2019 at 23:54
1

Use this code:

myObj.variableOne['someValue'] = 'new value';

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.