Currently, I am developing an RPG character generator that utilizes Redux. Within my reducer function, I aim to have the ability to update gear modifications and keep track of these changes. These modifications may include a rating, although it is not mandatory.
If the rating happens to be undefined, I would prefer not to store that information. Is there a way to exclude a key in object literal notation if the value is undefined?
The variable gearName will contain the name of the specific gear being modified.
In addition, mod is an object that includes details about the modification, such as +rating for fire resistance or other attributes.
Furthermore, rating represents a numerical value indicating the extent of the modification.
const purchaseGear = (state, action) => {
const { gearName, mod, rating } = action.parameter;
switch (action.type) {
case MOD_GEAR:
return {
...state,
[gearName]: {
...state[gearName],
mods: {
...state[gearName].mods,
[mod.name]: {
...mod,
rating
}
},
cost: mod.cost * (rating || 1)
}
}
}