0

I have following code

 if(ssnFlag == false){
                console.log(userName)
                userSessions.push({userName : [{"sessionId" : dsid}]})
                console.log("pushing::" + JSON.stringify(userSessions[0]))
              }

the output is

OIC_PLAN_ADMIN_OPERATIONS
pushing::{"userName":[{"sessionId":"0000MTEJon9Fs1MLIMK6yf1Ry1Ob0004vf"}]}

Notice when userName value is OIC_PLAN_ADMIN_OPERATIONS then why in the next like the word userName is being pushed when i am expecting it to push OIC_PLAN_ADMIN_OPERATIONS

1 Answer 1

4

You have to add some brackets to turn it into a "Computed property name":

  { [userName]: /*...*/ }

If you omit them, it is the same as:

 { "userName": /*...*/ }
Sign up to request clarification or add additional context in comments.

2 Comments

What is this use of square braces called? I've never seen them used in an object key like this.
Disregard, I found it. It was easier to Google than I expected. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.