Home > Enterprise >  How can I change the sequence of output values from JSON to FlatList?
How can I change the sequence of output values from JSON to FlatList?

Time:07-13

I'm new to React Native and I don't understand how to implement changing the sequence of output values from JSON format to FlatList. Let 's say my order in JSON goes like this: [id=1, name="test1"] , [id=2, name="test2"] and in FlatList it outputs from 1 to 2, and I need to reverse the output from 2 to 1. How do I do this?

  searchNews = async () =>
  {
    const ertAPI = await fetch(`${e_glav.e_url}/api/news/read.php?nocache`)
    const APIValue = await ertAPI.json();
    const APIResults = APIValue.ertnews
    this.setState({
      data:APIResults,
      isFetching: false
    })
  }

            <FlatList 

              data={this.state.data}
              onRefresh={() => this.onRefresh()}
              refreshing={this.state.isFetching}
              initialNumToRender={4} 
              contentContainerStyle={{ alignSelf: 'stretch' }}
              keyExtractor={({ id }, index) => id} 
              renderItem={({item}) => (

CodePudding user response:

If you are looking for a custom sequence, you have to send a sequence from the backend(API), basically, send sorted data from API or write a function to generate an array in your desired sequence.

If you just want to reverse, then do the following

this.setState({
  data:APIResults.reverse(), // <-- do this
  isFetching: false
})

Document to refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

Please mark as an answer if it worked!

  • Related