Javascript 如何从数组中获取特定值
您好,我需要有关如何从web服务中的数组中获取特定值的帮助。我正在使用fetch方法获取数据。在XML中,我正在使用依赖项将XML数据转换为JSONJavascript 如何从数组中获取特定值,javascript,react-native,Javascript,React Native,您好,我需要有关如何从web服务中的数组中获取特定值的帮助。我正在使用fetch方法获取数据。在XML中,我正在使用依赖项将XML数据转换为JSON 从“React”导入React; 从“react native”导入{样式表、视图、ActivityIndicator、平面列表、文本、TouchableOpacity}; 导出默认类源扩展React.Component{ 静态导航选项=({navigation})=>{ 返回{ 标题:“源代码列表”, 头型:{背景颜色:#fff}, heade
从“React”导入React;
从“react native”导入{样式表、视图、ActivityIndicator、平面列表、文本、TouchableOpacity};
导出默认类源扩展React.Component{
静态导航选项=({navigation})=>{
返回{
标题:“源代码列表”,
头型:{背景颜色:#fff},
headerTitleStyle:{textAlign:“中心”,flex:1}
};
};
建造师(道具){
超级(道具);
此.state={
加载:false,
项目:[]
};
}
FlatListItemSeparator=()=>{
返回(
);
}
renderItem=(数据)=>
{data.item.name}
{data.item.email}
{data.item.company.name}
render(){
{
if(this.state.loading){
报税表(
)}}
返回(
this.renderItem(项目)}
keyExtractor={item=>item.id.toString()}
/>
)}
}
const parseString=require('react-native-xml2js')。parseString;
取('http://192.168.200.133/apptak_service/apptak.asmx/Get_Item_Master')
.then(response=>response.text())
。然后((响应)=>{
parseString(响应、函数(错误、结果){
console.log(响应)
});
}).catch((错误)=>{
console.log('fetch',err)
这是fetchdata();
})
const styles=StyleSheet.create({
容器:{
弹性:1,
背景颜色:“fff”
},
加载器:{
弹性:1,
辩护内容:“中心”,
对齐项目:“中心”,
背景颜色:“fff”
},
名单:{
填充垂直:4,
差额:5,
背景颜色:“fff”
}
});代码>从您的代码中我可以看出,您没有将获取的数据传递到您的状态。您只在控制台中记录它:
parseString(response, function (err, result) {
console.log(response)
});
我认为您应该在组件中添加以下内容:
一,。首先,设置要在构造函数中调用的函数,以便它可以访问状态:
constructor(props) {
super(props);
this.state = {
loading: false,
items:[]
};
this.fetchRequest = this.fetchRequest.bind(this)
}
在渲染
中创建实际函数:
fetchRequest() {
fetch('http://192.168.200.133/apptak_service/apptak.asmx/Get_Item_Master')
.then(response => response.text())
.then((response) => {
parseString(response, function (err, result) {
this.setState({ items: response });
});
}).catch((err) => {
console.log('fetch', err)
})
}
您需要调用fetchRequest
函数。您可以通过以下方式完成此操作:
最后一件事是正确创建平面列表
:
<FlatList
data= {this.state.items}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor= {item=>item.id.toString()}
/>
}
keyExtractor={item=>item.id.toString()}
/>
您的数据源是this.state.items
,而不是this.state.dataSource
不幸的是,我不知道您的数据是什么样子的,所以我不知道应该如何编写密钥提取程序和
。我可以告诉你的是,你需要为你的项目唯一的ID
您可以在。发布您想要使用的数据样本HI和欢迎使用SO中阅读更多关于Flatlist
!你的问题太广泛了,如果没有更多细节,我们将无法帮助你。请阅读本指南,以帮助您编写高质量的问题,并提供高质量的答案:@Alexy使用相关代码更新了我的问题获取\u Item\u Master返回一个数组?@Ashoka是这将返回我终端中的元素数组
<FlatList
data= {this.state.items}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor= {item=>item.id.toString()}
/>