简体   繁体   中英

Accessing javascript object with mixed key square brackets

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?

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

Access them with

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

Did you try this?

billing = parameters.billing.firstname;

Or

billing = parameters.billing.firstname?

They are sending you a weird object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM