简体   繁体   中英

How do I access all values from nested JSON Array?

Here is my JSON array:

    [{"id":"11",
"first_name":"John",
"last_name":"Doe",
"username":"Username1234",
"email":"example1426@gmail.com",
"who":"Person",
"user_images":[{"id":"114",
"username":"Hannah_L123",
"images":"resized_1522-20131219-creativity.jpg","date":"12\/04\/2017"}]}]

I don't use the first array, but I do use the second array like this:

  user_image:  responseJson[0].user_images.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) }))

I just want to access all the username from user_images that is already mapped. Trying these did not work: {this.state.user_image.username} //Undefined {this.state.user_image["username"]} //Undefined

What am I doing wrong?

EDIT : I found out how to do it: this.state.user_image[0].username , but how do I console.log() all of the data that contains a username ? I am only logging 3 from the console.

React Native Code:

fetch('https://www.example.com/React/user.php')
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({
          user_image:  responseJson[0].user_images.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) })),
          }, function() {
            const userImagesWithUsername = this.state.user_image.map(item => item.username != undefined);
            console.log(userImagesWithUsername);
        });
      })
      .catch((error) => {
        //console.error(error);
      });

要获取包含username所有数据,您可以过滤user_image数组。

const userImagesWithUsername = this.state.user_image.filter(item => item.username != undefined);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM