1

Here is my JSON array:

    [{"id":"11",
"first_name":"John",
"last_name":"Doe",
"username":"Username1234",
"email":"[email protected]",
"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);
      });
2
  • did you use JSON.parse("your json") before trying to access the object? Commented Aug 15, 2018 at 16:25
  • No I did not, how do I do that? Because I never had a time where I would need to use JSON.parase @slashsharp Commented Aug 15, 2018 at 16:27

1 Answer 1

1

To obtain all data that contains username you can filter the user_image Array.

const userImagesWithUsername = this.state.user_image.filter(item => item.username != undefined);
Sign up to request clarification or add additional context in comments.

6 Comments

thank you, but When I console log this all it does it print the whole list out again.
If the whole list contains a username field I think it is normal! What do you really want to do?
I just want all of the usernames from that list. Just that without all of the other properties, if it's possible.
Ah okay! Then just add .map(item => item.username) and it’ll return a array of username
fetch('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) => { });
|

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.