0

I'm trying to access elements of a javascript object. I have no control over how it has been created.

An example is as follows:

parameters = Object { checkout_method="guest",  billing[firstname]="fasfdas",  billing[lastname]="fdsa" }

this works fine -

var checkout = parameters.checkout_method;

however trying to access any of the billing ones results in either an error or an 'undefined'

var billing = parameters.billing['firstname'];
var billing = parameters['billing.firstname'];

basically I've crawled stackoverflow and tried about 20 different combinations to get the values with no result.

Does anyone have any idea how you access an object key with mixed square bracketed content?

15
  • 1
    Does parameters['billing[firstname]'] return the correct result? Commented Dec 12, 2016 at 17:33
  • 1
    var billing = parameters["billing[firstname]"]; Commented Dec 12, 2016 at 17:34
  • 1
    please add valid javascript code for parameters. Commented Dec 12, 2016 at 17:34
  • 1
    How about this? Commented Dec 12, 2016 at 17:35
  • 1
    As other have suggested.. does parameters["billing[firstname]"]; work? Commented Dec 12, 2016 at 17:36

2 Answers 2

1

Confusingly, the object you have contains key names that contain square brackets.

Access them with

var billing = parameters['billing[firstname]'];
Sign up to request clarification or add additional context in comments.

1 Comment

Ya that might be it. It is weird the object. Using the brackets with it inside the quotes might be correct.
0

Did you try this?

billing = parameters.billing.firstname;

Or

billing = parameters.billing.firstname?

They are sending you a weird object.

1 Comment

He said he tried parameters.billing['firstname'], which is the same as what you suggest.

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.