apasna0
apasna0

Reputation: 39

Reactjs passing uppercase dynamic variable

I have a problem passing dynamic variable to other component.
I have array of objects, which have other objects inside:

var myArr = {"1": {
"name": "rose", 
"symbol":"ros", 
"quotes": { 
    "USD": {
        "price": "10"
    }
}}}

This is how I am trying to pass variables to my component:

<MyComponent
 key={index}
 name={item.name}
 symbol={item.symbol}
 price={item.quotes.USD.price}
/>

Doing like this, I get error:

TypeError: Cannot read property 'USD' of undefined

Also I tried to pass price variable like this:

<MyComponent
 key={index}
 name={item.name}
 symbol={item.symbol}
 price={item.quotes.${USD}.price}
/>

But got Failed to compile error:

Failed to compile
price={item.quotes.${USD}.price}
                   ^

How can I pass dynamic variable which contains uppercase?

Upvotes: 1

Views: 79

Answers (2)

Wysiati
Wysiati

Reputation: 139

You might want to do something like this:

var myArr = {"1": {
"name": "rose", 
"symbol":"ros", 
"quotes": { 
"USD": {
    "price": "10"
}
}}}

myArr['1'].quotes.USD.price

(or)

myArr['1'].quotes['USD'].price

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

After carefully reviewing your code I found that you're missing the index:

price={item[index].quotes.USD.price}

Upvotes: 1

Related Questions