【发布时间】:2020-08-15 19:23:22
【问题描述】:
我有这个函数,它应该循环遍历(smn)数组的 20 个元素以显示满足 IF 条件的元素,使用这段代码我只得到了第一个数组元素:
renderSMN() {
const smn = ['fb', 'twt', 'snp', 'ins', 'ytu', 'lnk', 'tik', 'beh', 'pin', 'tmp', 'www', 'eml', 'wap', 'msg', 'tlg', 'lin', 'wch', 'skp', 'snd', 'oth'];
for (let i = 0; i < 20; i++) {
//alert(this.state.data.smu_+smn[i]);
alert(i);
if (this.state.data.smu_+smn[i] != "") {
return (
<View style={{ margin: 5 }}>
<TouchableOpacity onPress={() => {
this.props.navigation.navigate('Result', { postId: item.uid, otherParam: 'Pass whatever you want here', });
}}>
<Image source={{ uri: 'http://localhost/rest/smn/' + smn[i] + '.png' }} style={{ width: 100, height: 100, right: 0, }} />
</TouchableOpacity>
<Text>{i}</Text>
</View>
);
}
这是我从 JSON 文件中获取数据的方式:
fetch('http://localhost/rest/api/single_read.php/?id=' + postId, {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
this.setState({
data: responseJson
})
})
.catch((error) => {
console.error(error);
});
【问题讨论】:
标签: javascript reactjs react-native for-loop