I'm a beginner for the React native
. Right now I'm trying to develop a mobile application and I have some issues about storing the JSON
data into an array list. For example, the JSON
data looks like this
[{"ID":"1", "Name":"wonderful"},{"ID":"2","Name":"happy"}].
And right now I'm using a third-party library which is called react-native-tag-select
. To use this library, the data must be stored in this way,
const data = [
{ id: 1, label: 'Money' },
{ id: 2, label: 'Credit card' },
{ id: 3, label: 'Debit card' },
{ id: 4, label: 'Online payment' },
{ id: 5, label: 'Bitcoin' },
];
In this one, the props for the id is a string and also label is a string.
So how can I just store my JSON
data separately like this? For example, like this
id:1, name: "wonderful".
I have tried to use the map method to do this. The code will be
this.state.data.map(function(data,index){
return data.ID})
but it's still not working.
The code is in the below:
export default class select_page extends Component {
constructor() {
super()
this.state = {
data: [],
}
}
//updateSearch = search => {
// this.setState({ search });
//};
componentDidMount(){
return fetch('url')
.then((response) => response.json())
.then(response => this.setState({data:response}))
.catch((error) => {
console.error(error);
});
}
render() {
var getId=this.state.data.map(function(data,index){
return data.ID
})
var getlabel=this.state.data.map(function(data,index){
return data.Name
})
const { search } = this.state;
const data=[{id:getId,label:getlabel}]
return (
<View style={styles.TagSelected}>
<Text style={styles.labelText}>Select Name from below:</Text>
<SearchBar
inputStyle={{ backgroundColor: 'white' }}
inputContainerStyle={{ backgroundColor: 'white', borderRadius: 10 }}
containerStyle={{ backgroundColor: '#ecf0f1', borderBottomColor: 'transparent', borderTopColor: 'transparent' }}
placeholder="Search for a trait"
onChangeText={this.updateSearch}
value={search}
/>
<TagSelect
data={data}
ref={(tag) => {
this.tag = tag;
}}
/>
</View>
)