在JSX/React Native中将变量与JSON连接



因此,我正在构建一个基于天气条件的自定义报价的天气应用程序。以下代码有效。。。但正如你所看到的,我有多个引号。我生成了一个随机数变量,它也起作用。我正在尝试将该变量添加到weatherCasesJSX。。。我知道这是可以做到的,但伙计,我不知道让它工作的语法。。。

报价工作

const weatherCases = {
"clear sky": {
title: "clear sky",
quote1: "Custom quote 1.",
quote2: "Custome quote 2",
quote3: "Custom quote 3",
...
}
}
function Forecast({ temp, weatherName, city }) {
return ( 
<View style={styles.forecastContainer}> 
<View style={styles.forecastTopContainer}>
<View>
<Text style={styles.quoteText} >{weatherCases[weatherName].quote}</Text>
</View>
</View>
</View>
);
}

我试图做的是获得随机报价(这是为使该行工作而进行的十几次编辑之一:

function Forecast({ temp, weatherName, city }) {
const randomNumber = (Math.floor(Math.random() * 30));
console.log(randomNumber);
//console.log(weatherName);
return ( 
<View style={styles.forecastContainer}> 
<View style={styles.forecastTopContainer}>
<View>
<Text style={styles.quoteText} >{weatherCases[weatherName].quote.{randomNumber}}</Text>
</View>
</View>
</View>
);
}

我相信你正在努力成为这样的

weatherCases[weatherName]["quote"+randomNumber.toString((]

例如,结果randomNumber是3,它将像这个

weatherCases["clear sky"]["quote3"]

最新更新