0

I want to map my JSON data into flatlist so that i can list out my data from database. is it possible to do so or are there any other ways to do it? I had tried this for whole day and hope could be fix now.

This is my JSON:

{"-M1ytAolVL1xdnO0dXjD": {"Favourite": false, "ItemIdentity": "Qjwj", "ItemName": "Sjsj", "ItemQuantity": "1"}, "-M1ytGOJD62TwwEWOmMu": {"Favourite": true, "ItemIdentity": "This is my thing", "ItemName": "Acer laptop", "ItemQuantity": "12"}}

This is my flatlist code:

    render() {
        console.log(this.state.retrieveData.id)
        return (
            <View>
                <Text>A</Text>   
                    <FlatList data={this.state.retrieveData}
                        renderItem={({ item }) => (
                            <ListItem title={item.Favourite} />
                        )}
                    >
                        <Text>B</Text>
                    </FlatList>
            </View>
        )
    }```



3
  • Maybe get rid of "-M1ytAolVL1xdnO0dXjD", "-M1ytGOJD62TwwEWOmMu"... these, so that your json looks like this [{"Favorite":false, ...}, {"Favourite": true,...}] Commented Mar 10, 2020 at 7:16
  • but that is the unique id from firebase Commented Mar 10, 2020 at 7:18
  • 1
    then you can make it [{"id": "-M1ytAolVL1xdnO0dXjD", "Favorite" : false,.....},...], using loop. Flat-list will take only arrays Commented Mar 10, 2020 at 7:21

3 Answers 3

1

you need to parse your json using JSON.parse() in order to use it for the flatlist, also Flatlist component only accepts arrays and not the objects. If you want to keep the object keys returned by the firebase try formatting your data first.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like this -

constructor(props){
    super(props);
    this.state = {retrieveData: []};
}

componentDidMount() {
    // fetch your json here
    const json = {"-M1ytAolVL1xdnO0dXjD": {"Favourite": false, "ItemIdentity": "Qjwj", "ItemName": "Sjsj", "ItemQuantity": "1"}, "-M1ytGOJD62TwwEWOmMu": {"Favourite": true, "ItemIdentity": "This is my thing", "ItemName": "Acer laptop", "ItemQuantity": "12"}};
    const list = [];
    for(const k in json) {
      list.push({
        id: k,
        ...json[k],
      })
    }
    this.setState({retrieveData: list});
}

Then

render() {
        return (
            <View>
                <Text>A</Text>   
                    <FlatList data={this.state.retrieveData}
                        renderItem={({ item }) => (
                            <ListItem title={item.Favourite} />
                        )}
                    >
                        <Text>B</Text>
                    </FlatList>
            </View>
        )
    }

Comments

1

For the very first you could parse raw JSON string:

const rawData = JSON.parse('{"-M1ytAolVL1xdnO0dXjD": {"Favourite": false, "ItemIdentity": "Qjwj", "ItemName": "Sjsj", "ItemQuantity": "1"}, "-M1ytGOJD62TwwEWOmMu": {"Favourite": true, "ItemIdentity": "This is my thing", "ItemName": "Acer laptop", "ItemQuantity": "12"}}')

then transform into array of values:

const data = Object.values(rawData);
/*
// will produce 
  [
{"Favourite": false, "ItemIdentity": "Qjwj", "ItemName": "Sjsj", "ItemQuantity": "1"}, 
{"Favourite": true, "ItemIdentity": "This is my thing", "ItemName": "Acer laptop", "ItemQuantity": "12"}
]
*/

Important: You should not do it inside render(). Fill free to move such transformation near to fetching.

And then use data in render:

render() {
        console.log(this.state.data)
        return (
            <View>
                <Text>Data</Text>   
                    <FlatList data={this.state.data}
                        renderItem={({ item }) => (
                            <ListItem title={item.Favourite} />
                        )}
                    />
            </View>
        )
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.