Javascript 使用条件语句表示数组的类型
我正在构建一个react本地应用程序,它使用检查货币对的买卖价格是否达到用户设置的阈值,然后发送用户设置的消息。有两个价格阈值是有条件提供的:BuyThreshold price,即如果用户希望得到通知的价格等于当前市场购买价格,例如货币对的1.12343,例如EURUSD。另一个阈值是销售阈值,即如果用户希望得到通知的价格等于当前市场销售价格,例如货币对的1.12330,例如欧元/美元 该功能不检查市场买入价或卖出价是否达到阈值。我怎么做?代码如下:Javascript 使用条件语句表示数组的类型,javascript,arrays,reactjs,react-native,for-loop,Javascript,Arrays,Reactjs,React Native,For Loop,我正在构建一个react本地应用程序,它使用检查货币对的买卖价格是否达到用户设置的阈值,然后发送用户设置的消息。有两个价格阈值是有条件提供的:BuyThreshold price,即如果用户希望得到通知的价格等于当前市场购买价格,例如货币对的1.12343,例如EURUSD。另一个阈值是销售阈值,即如果用户希望得到通知的价格等于当前市场销售价格,例如货币对的1.12330,例如欧元/美元 该功能不检查市场买入价或卖出价是否达到阈值。我怎么做?代码如下: const [clickedi
const [clickedindex, setClickedIndex] = useState(0)
function checkCondition({BuyThreshold, SellThreshold, SMSMessage}) {
const BuyingPrice = {...currency.data.prices[clickedindex].BuyingPrice}
const SellingPrice = {...currency.data.prices[clickedindex].SellingPrice}
const currencypair = {...currency.data.prices.instrument}
const BuyThresholdarray = []
const SellThreshhold =[]
BuyThresholdarray.push(BuyThreshold)
const interval = setInterval(() => {
for (let i = 0; i < BuyThresholdarray.length; i++) {
console.log(BuyThresholdarray[i])
if(SelectedCurrencyPair) {
if (BuyingPrice >= BuyThresholdarray[i]) {
return(
console.log(SMSMessage)
)
}
else {
console.log("ALert ")
}
}
}
}, 1000);
<View>
{
// Mapping of the actual currency pairs and their ask and bid prices respectively
currency.data.prices && currency.data.prices.map((prices, index) => {
return (
<ListItem
key={index}
onPress = {() => {setModalOpen(true);setClickedIndex(index);}}
bottomDivider>
<ListItem.Content>
<ListItem.Title>
{data.prices[index].instrument} {data.prices[index].BuyingPrice} {data.prices[index].SellingPrice}
</ListItem.Title>
</ListItem.Content>
</ListItem>
)
})
}
</View>
// Modal with textinputs for the Buy and Sell Thresholds
<Modal
visible={modalopen}
transparent={true}
animationType={"fade"}
>
<Text style={{textAlign: "center", fontWeight: "bold"}}>
Create Alert On:
</Text>
<Text style={{textAlign: "center", fontWeight: "bold"}}>
{data.prices[clickedindex].instrument}
</Text>
<Text style={{textAlign: "center"}}>
{data.prices[clickedindex].BuyingPrice}/{data.prices[clickedindex].SellingPrice}
</Text>
<Card.Divider/>
<View>
<View>
<Picker
mode= "dropdown"
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) =>setSelectedValue(itemValue) }
>
<Picker.Item label="BuyPrice" value="BuyPrice" />
<Picker.Item label="SellPrice" value="SellPrice" />
</Picker>
</View>
{
selectedValue === "BuyPrice" ?
<TextInput
style={styles.textInputStyle}
value={BuyThreshold}
onChangeText = {(BuyThreshold) => setBuyThreshhold(BuyThreshold)}
placeholder="BuyThreshhold"
placeholderTextColor="#60605e"
numeric
clearButtonMode='always'
keyboardType='decimal-pad'
/> :
<TextInput
style={styles.textInputStyle}
value={SellThreshold}
onChangeText = {(SellThreshhold) => setSellThreshhold(SellThreshhold)}
placeholder="Sell Threshhold"
placeholderTextColor="#60605e"
numeric
clearButtonMode='always'
keyboardType='decimal-pad'
/>
}
</View>
<View style={{ flexDirection: "row", justifyContent:"center" }}>
<View style={styles.inputWrap}>
<TextInput
style={styles.messageStyle}
value={SMSMessage}
onChangeText = {(SMSMessage) => setSMSMessage(SMSMessage)}
placeholder="Message"
clearButtonMode='always'
placeholderTextColor="#60605e"
/>
</View>
</View>
<View style={{flexDirection: "row", justifyContent: "center"}}>
<View>
<TouchableOpacity style={styles.button}
onPress={() => {
if(SMSMessage.length === 0)
{
Alert.alert("Incomplete", "Enter your Alert Message")
return ;
}
createAlert();
checkCondition({BuyThreshold, SellThreshold, SMSMessage});
setModalOpen(false);
showToastWithGravityAndOffset();} }
>
<Text style={styles.buttonTitle}>OK</Text>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity style={styles.button} onPress ={() => setModalOpen(false)}>
<Text>Close</Text>
</TouchableOpacity>
</View>
</View>
</View>
</View>
</Modal>
const[clickedindex,setClickedIndex]=useState(0)
函数检查条件({BuyThreshold,SellThreshold,SMSMessage}){
const BuyingPrice={…currency.data.prices[单击索引].BuyingPrice}
const SellingPrice={…currency.data.prices[单击索引].SellingPrice}
const currencypair={…currency.data.prices.instrument}
常量BuySthresholdArray=[]
常数SellThreshold=[]
BuySthresholdArray.push(BuySthreshold)
常量间隔=设置间隔(()=>{
for(设i=0;i=BuyingThresholdArray[i]){
返回(
console.log(SMSMessage)
)
}
否则{
控制台日志(“警报”)
}
}
}
}, 1000);
{
//实际货币对的映射及其分别的买卖价格
currency.data.prices&¤cy.data.prices.map((价格,指数)=>{
返回(
{setModalOpen(true);setClickedIndex(index);}
底部分隔器>
{data.prices[index].instrument}{data.prices[index].BuyingPrice}{data.prices[index].SellingPrice}
)
})
}
//具有用于购买和出售阈值的文本输入的模式
在以下情况下创建警报:
{data.prices[clickedindex].instrument}
{data.prices[clickedindex].BuyingPrice}/{data.prices[clickedindex].SellingPrice}
setSelectedValue(itemValue)}
>
{
selectedValue==“购买价格”?
SetBuyThreshold(购买阈值)}
占位符=“BuySthreshold”
占位符textcolor=“#60605e”
数字的
clearButtonMode='always'
键盘类型=“十进制键盘”
/> :
设置SellThreshold(SellThreshold)}
占位符=“出售阈值”
占位符textcolor=“#60605e”
数字的
clearButtonMode='always'
键盘类型=“十进制键盘”
/>
}
设置短信(短信)}
占位符=“消息”
clearButtonMode='always'
占位符textcolor=“#60605e”
/>
{
如果(SMSMessage.length==0)
{
警报。警报(“不完整”,“输入您的警报消息”)
返回;
}
createAlert();
checkCondition({buystreshold,SellThreshold,smsmsmessage});
setModalOpen(假);
showtoast with gravityandoffset();}
>
好啊
setModalOpen(假)}>
接近
在哪里定义了SelectedCurrencyPair
?这应该是currencypair
?这是无效的javaScript:const SellingPrice={data.prices.SellingPrice}
,还有你的问题是什么?我已经编辑了这个问题,你希望SellingPrice
和BuyingPrice
成为数组吗?我想您应该将这些设置为:const{SellingPrice,BuyingPrice}=data.prices
您还需要检查是否(SelectedCurrencyPair.length>0){
,因为空数组在JavaScript中是一个真实值。这是一个开始。需要更多信息才能完全解决(您能否创建一个具有类似虚拟数据的功能代码笔)?让我为上下文添加模式代码