Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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}) => (

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading