I have 3 arrays of elements(Users, Comments, Ratings) and I need for 2 of them to output the coresponding value from the first. Something like:
functionComments(users_id)
{
returns the coresponding comments after iterating through}
functionRatings(users_id){returns the coresponding ratings after iterating}
{this.state.Users.map(u=>{
{<Card >{functionComments(u.id)<Card/>}
{<Card >{functionRatings(u.id)<Card/>}
})}
But I have no idea how to return the value without using this.state
elements in a User array look like this
Users: {
"firstName": "diana",
"lastName": "diana",
"age": "0",
"email": "mari@yahoo.com",
"password": "123456",
"name": "diana",
"username": "Rn9h8fJfs1XvTv1Y9Sr1mXWB7ib2",
"description": "12346",
"address": null,
"id":45
}
Comments:
{
"username": "Rn9h8fJfs1XvTv1Y9Sr1mXWB7ib2",
"commentsList": [
{
"comment": "loc",
"id": 40,
"username": "Rn9h8fJfs1XvTv1Y9Sr1mXWB7ib2",
"movieID": "tt6048922"
}
]
}
Ratings
{
"username": "Rn9h8fJfs1XvTv1Y9Sr1mXWB7ib2",
"ratingsList": [
{
"rating": 5,
"id": 50,
"username": "Rn9h8fJfs1XvTv1Y9Sr1mXWB7ib2",
"movieID": "tt3089630"
}
]
}
The output JSON I want to get:
{
username: usernameleters,
firstName: userFirstName,
commentsList:[
{
comments:"comm"
movieID:"movie"
}],
ratingsList:[
{
rating:"rrt"
movieID:"mm"
}]
}
The fetches for the 3 arrays look like this :
async reloadObjectiveList() {
await UserApiService.fetchUsers()
.then((res) => {
this.setState({
Users: res.data.result
})
});
await CommentApiService.fetchComments()
.then((res) => {
console.log( res.data.result)
this.setState({
Comments: res.data.result
})
}); console.log(this.state.Comments)
await RatingApiService.fetchrating()
.then((res) => {
console.log( res.data.result)
this.setState({
Ratings: res.data.result
})
});
}
componentDidMount()
{
this.reloadObjectiveList();
const userData = (this.mergeData(this.state.Users, this.state.Comments, this.state.Ratings));
console.log("user data "+ this.userData)
this.setState({UserData:this.userData})
}