Reputation: 593
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);
});
Upvotes: 1
Views: 153
Reputation: 944
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);
Upvotes: 1