0

I have API data in my React component. It's in the form of

{weather.Wind.Speed.Metric.Value}

Is there any way I can swap Metric out for a variable such as unit.

For example something like

const unit = 'Metric';

{weather.Wind.Speed.${unit}.Value}

That way I can update the variable and show the correct data?

1 Answers1

1

You can use the bracket notation: weather.Wind.Speed[unit].Value.

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

domenikk
  • 1,723
  • 1
  • 10
  • 12