I am a little stuck in here, so basically the problem is with the nested map function. I have main map function which provides me a list of all hotels ever added to the database. Inside that map function, I must have a nested map of reviews for specified hotel but, the problem is with the same keys. In main map function, I have keys like, id, name, etc. As for the nested review map, I have id, which is basically different from the id provided in main map so I'm stuck while trying to connect them.
Here's my code:
<div>
<div className='landing-container'>
<nav>
<h2>Hotel Challenge</h2>
<ul>
<li><a className="nav-link" onClick={loadHotels}>List All Hotels</a></li>
<li><Link className='nav-link' to='/dashboard'>Dashboard</Link></li>
<li><Link className='nav-link' to='/favorites'>Favorites</Link></li>
<li><a className="nav-link" onClick={handleLogout}>Logout</a></li>
</ul>
</nav>
</div>
<div className="layout-container">
<div>{hotelItems.map((item) => (
<div className="hotel-container" key={item.id}>
<div className="hotel-name">
{item.name}
<div className="hotel-location">{item.city}, {item.country}</div>
<div className="hotel-stars">
{Array(item.stars).fill(<FontAwesomeIcon className="icon-color-star" icon={faStar} />)}
</div>
<img className="hotel-image" src={item.image} />
<div className="reviews">
<button className="review-button" onClick={showHotelReviews}>Show Reviews</button>
{hotelReviews.map((review) => (
<div>
<div className="review-message">{review.id}</div>
<div className="review-likes">Liked by: <span>{review.likes}</span><sup><FontAwesomeIcon className="icon-color-likes" icon={faHeart} /></sup></div>
</div>
))}
</div>
</div>
</div>
))}</div>
</div>
</div>
I tried providing the main map key to the nested map, but that didn't work either.
All hotels in array:
[
{
"id": 42,
"name": "Courtyard by Marriott Belgrade City Center",
"city": "Belgrade",
"country": "Serbia",
"image": "http://127.0.0.1:8000/media/images/54129602.jpg",
"stars": 4,
"date": "2017-11-15 00:03:32",
"description": "Lorem Ipsum",
"price": 52.0,
"likes": 45,
"dislikes": 2,
"user": [
1,
73
],
"location": "44.8170754,20.4580087"
},
]
Reviews Array:
[
{
"id": 1,
"message": "“Very comfortable.”",
"created_at": "2020-08-13T18:10:56.240421Z",
"likes": 5,
"dislikes": 0,
"positive": true,
"author": {
"id": 1,
"first_name": "First Name",
"last_name": "Last Name"
}
}
]
hotelItemsandhoteReviewsarrays