To establish the values for your defaultListingFormValues
including categories such as objects
, you can use the structure
{ itemWeight: 1, dress: { itemWeight: 1 },... }
.
Utilize these values based on the selected category
from a dropdown menu. If the categoryFromDropDown
references a category like dress
, the code will fallback to a default
value if the category is not explicitly defined.
let defaultValue = defaultListingFormValues[categoryFromDropDown] ?
defaultListingFormValues[categoryFromDropDown].itemWeight :
defaultListingFormValues.itemWeight;
Experiment with this concept below. The usage of export
& import
statements are included as comments.
// export const defaultListingFormValues = {
const defaultListingFormValues = {
itemWeight: 1,
dress: {
itemWeight: 0.5
},
heels: {
itemWeight: 1
},
boots: {
itemWeight: 2
}
}
// When you access
// import defaultListingFormValues from '...';
let categoryFromDropDown = 'dress';
let defaultValue = defaultListingFormValues[categoryFromDropDown] ?
defaultListingFormValues[categoryFromDropDown].itemWeight :
defaultListingFormValues.itemWeight;
console.log(defaultValue);
// category which is not present in defaultListingFormValues.
categoryFromDropDown = 'aaa';
defaultValue = defaultListingFormValues[categoryFromDropDown] ?
defaultListingFormValues[categoryFromDropDown].itemWeight :
defaultListingFormValues.itemWeight;
console.log(defaultValue);