Assign value based on 100s of switch statement In the following code I have potentially hundreds of possible cases. (This is just a small subset.) Is there some way to more effectively set state.dimensionQty based on the string held in state.dimensionType? For example, some kind of function or loop, that would make switch less imperative?

Simply put, state.dimensions contains all possible dimension measurement, and the user has specified a dimensionType that they would like to save.

Example is Vue.js code within Vuex, but the general case is relevant too.

switch (state.dimensionType) {
    case 'quantity':
      state.dimensionQty = state.dimensions.quantity
      break
    case 'in':
      state.dimensionQty = state.dimensions.in
      break
    case 'm':
      state.dimensionQty = state.dimensions.m
      break
    case 'cm':
      state.dimensionQty = state.dimensions.cm
      break
    case 'ft':
      state.dimensionQty = state.dimensions.ft
      break
    case 'yd':
      state.dimensionQty = state.dimensions.yd
      break
    default:
      throw new Error(
        'No dimension type specified.'
      )
  }
If state.dimensionType is always an exact key match for a value in state.dimensions, and assuming that the default means the key doesn't exist, then you can do a lookup, for example:

if (!state.dimensions.hasOwnProperty(state.dimensionType)) {
  throw new Error('No dimension type specified');
}

state.dimensionQty = state.dimensions[state.dimensionType];