Reputation: 39
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
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
Reputation: 85545
After carefully reviewing your code I found that you're missing the index:
price={item[index].quotes.USD.price}
Upvotes: 1