Filtering A Mapped Object Based On React Router
I'm trying to filter my map array by the :reviewId in my router(v4) url the code works if I set it manually to the routeId parameter {review.items.filter(item => item.id === 2)`
Solution 1:
I think, this is a type mismatch issue, this.props.match.params.reviewId
will return a string
value, and id is an integer
value.
So instead of ===
use ==
.
Like this:
item.id == this.props.match.params.reviewId
Reason:===
checks the value and type both.
To verify this use:
console.log(typeOf(this.props.match.params.reviewId), typeOf(item.id))
inside filter
and check the result.
Try this:
render() {
const revs = reviews.map(review =><div><divkey={review.id} />
{
review.items.filter(item => {
console.log(typeOf(this.props.match.params.reviewId), typeOf(item.id))
return item.id == this.props.match.params.reviewId;
})
.map(item =>
<divclassName="cell"><divkey={item.id}><imgsrc={item.img}alt={item.name} /><div>{item.text}</div></div></div>
)}
</div>
)
Post a Comment for "Filtering A Mapped Object Based On React Router"