0

I am displaying reviews from the Google Maps API. Within my map function I only want to display reviews that are higher than a 1 rating. How can I update my map function to only display reviews where review.rating > 1?

{this.state.reviews && this.state.reviews.map((review, index) => (
    <div key={index} className="review">
        <p>{review.text}</p>
    </div>
))}
1
  • 1
    instead of just immediately returning the JSX you could change it to a regular arrow function and run a check like if(review.rating ==1) return null; and then just wrap the rest of the JSX in a return statement as well Commented Dec 9, 2019 at 0:07

2 Answers 2

6

Just filter them before map:

{this.state.reviews
    .filter((review) => review.rating > 1) // get only reviews with rating > 1
    .map((review, index) => (
       <div key={index} className="review">
          <p>{review.text}</p>
       </div>
    ))
}
Sign up to request clarification or add additional context in comments.

Comments

3

If, for some reason you don't want to call the .filter method on the array, you could do something like this. Basically you are calling a ternary operator in your return. Let me know if you need any more explanation.

{this.state.reviews && this.state.reviews.map((review, index) => (
  review.index > 1
   ? (<div key={index} className="review">
        <p>{review.text}</p>
    </div>)
   : null
))}

Comments

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.