1

The only data I can access right now is the beginning part of the array:

[
    {
        /*start*/
        "username" : "Bob",
        "password":"123456",
        "bio":"Hi",
        /*End*/
        "data":
        [
            {
                "pet" : "dog",
                "age" : "20",
                "city" : "5"
            },
            {
                "pet" : "cat",
                "age" : "18",
                "city" : "7"
            }
        ]
    }
]

I can also access part of the data array, but I am trying to access all of it. For example: {item.data[1].pet}. I tried using a for loop but was unsuccessful with that. I am also using react-native flat list and doing dataSource: responseJSON.data didn't work for me either. I'm sure there has to be a way to access all of the values of petinstead of just one each time, I just don't know how to.

Also since I am a beginner any tips will be gladly appreciated.

EDIT: Attempts at trying to loop from outside the JSX:

 var itemList = [];

for (var i = 0; i < responseJSON[0].data.length; i++) { itemList.push(<Text> {responseJson[0].data[i].pet}</Text>); }

I put that out of the render() Then I take itemList and stick it here:

<FlatList

          data={ this.state.dataSource}

          ItemSeparatorComponent = {this.FlatListItemSeparator}


          renderItem={({item}) => <View>

          {itemList}
          </View>
             }

          keyExtractor={(item, index) => index}

     />

Now it's not understanding that itemList is a variable. I'm getting a syntax error.

EDIT 2: Thanks to everyone the only thing I need help with is putting a variable inside of componentDidMountin React Native.

11
  • 1
    What is wrong with looping through data? I hope you know arrays start with 0 and not 1, right? Commented Feb 12, 2018 at 14:49
  • Can you post the code you are using to try to access this data? Commented Feb 12, 2018 at 15:25
  • Your renderItem function is trying to dump the itemList inside a View. You probably only want to render one item per renderItem. See if this helps you along renderItem={(item) => <View><Text>{item.pet}</Text></View>, Also, if the list will be changing you should read up on extraData FlatList prop. Commented Feb 12, 2018 at 16:09
  • @TravisWhite I did this for the data about data in the array and it works. I am trying to dump data's value like pets all in the JSX. Commented Feb 12, 2018 at 16:44
  • @LaneyWilliams if you know which properties the data may contain, just hardcode everything? {item.pet} {item.age} {item.city}? If you don't know which properties the data may contain, you can check this out stackoverflow.com/questions/8312459/… Commented Feb 12, 2018 at 16:51

4 Answers 4

1

I have no experience with React but from what I've found it seems it should be done this way:

const responseJson = [
    {
        /*start*/
        "username" : "Bob",
        "password":"123456",
        "bio":"Hi",
        /*End*/
        "data":
        [
            {
                "pet" : "dog",
                "age" : "20",
                "city" : "5"
            },
            {
                "pet" : "cat",
                "age" : "18",
                "city" : "7"
            }
        ]
    }
];


const listItems = responseJson[0].data.map((details) =>
  <li>{details.pet}</li>
);

ReactDOM.render(
  <ul>{listItems}</ul>,
  document.getElementById('root')
);

The output I get is:

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

1 Comment

Yes this definitely seems like it should work, but in React Native, the const is only allowed in the render(){}. I could research further more about that. @Andrew
1

You can do it like this in javascript:

var json = {"data":[{"pet":"dog","age":"20","city":"5"},{"pet":"cat","age":"18","city":"7"}]};


for (var i = 0; i < json.data.length; i++) {
  var object = json.data[i];
  console.log(object);
  console.log(object.pet, object.age, object.city);
}

1 Comment

The data is in the database
0

Try this loop :

for (var i = 0; i < responseJSON[0].data.length; i++) { 
    alert(responseJSON[0].data[i].pet);
}

Working example here :

https://plnkr.co/edit/c1sZYKrBWHFJOKwHEMbU?p=preview

2 Comments

Ok this is great, but I tried putting this into JSX. var itemList = []; for (var i = 0; i < responseJSON[0].data.length; i++) { itemList.push(<Text> {responseJson[0].data[i].pet}</Text>); } I keep getting a syntax error on var itemList= []; @Leo R.
@LaneyWilliams, add your attempts in your question, here they are much harder to understand and have much less visibility.
-1

Why don't you convert all the data in the responseJson to objects using Lodash Library and then take the data object from it and assign it to a variable and then convert it back to an array. And after that you change the data source to that array you get

See the Documentation of Lodash here

1 Comment

I didn't downvote, but why would he need such library?

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.