0

please am I getting data from an API everything is okay, and I also have a state that stores the currency the user enters but I want to get whatever currency the store on the state. but am getting undefined. how can write the cde

Here's how the data look when I console it.

console.log(data.market_data.current_price)
{
aed: 6160.74,
ars: 323915,
aud: 2440.2,
bch: 11.046617,
bdt: 178256,
}

if I want to get a part data

console.log(data.market_data.current_price.bdt)
// output: 178256

but I have a state like this

const [currency, setCurrency] = useState("ars")

here's my code

console.log(data.market_data.current_price.currency)

but am getting undefined

Emmanuel uzoezie
  • 433
  • 1
  • 5
  • 18
  • `console.log(data.market_data.current_price[currency])` – 0stone0 Feb 21 '23 at 13:57
  • Does this answer your question? [Accessing an object property with a dynamically-computed name](https://stackoverflow.com/questions/4244896/accessing-an-object-property-with-a-dynamically-computed-name) – 0stone0 Feb 21 '23 at 13:57
  • am still getting undefined but getting the price is I specify the currency in a string like this `console.log(data.market_data.current_price[`usd`]` but undefined like this `console.log(data.market_data.current_price[`${currency}`]` – Emmanuel uzoezie Feb 21 '23 at 14:09

0 Answers0