You've got the semantics wrong for what your code means. This:
shipping["only"]["price"] = $(this).val();
...sort of translates to:
- Evaluate
$(this).val() (we'll call this R for the sake of convenience)
- Evaluate
shipping["only"] (we'll call this O)
- Assign the
price property of O to the value R.
On step 2, because shipping is the value {}, shipping["only"] is undefined, and this is why you get the error that it can't assign to the property price of undefined.
If your intent is to create an object only property and set the price value of that new object, you'll want to do something more like:
const shipping = {
only: {
price: $(this).val()
}
};
...or you can do two assignment statements, one to populate .only and then one to populate the sub-property:
shipping.only = {};
shipping.only.price = $(this).val();
shipping["only"]is undefined. And looking at your shipping object, there is no properties in it.shipping.only = {price : $(this}.val()};?shipping.only = { ...shipping.only, price: $(this).val() }