0

I am trying to loop through JSON data and only show the first "4" results, but through all of my searching most of the answers I have seen use forEach() or map() which loops through all of the data. I have tried using for(), but it results in nothing displaying on the page. If anyone can help or direct me to an answer I haven't been able to find that would be great!

{this.state.products.map(function(product) {
    return(
        <div className="col-xs-12 col-sm-6 col-md-3" key={product.productId}>
            <img src={product.productImage1} Image1 alt="Logo"/>
            <h3>{product.productTitle}</h3>
            <p>{product.productDesc}<a href="/">Learn More</a></p>
        </div>
    );
})}
4
  • May be you should use index parameter in map method- like ( if index < 4 ...) Commented Mar 23, 2017 at 11:17
  • Do you have a link that references map method referencing index? Commented Mar 23, 2017 at 11:52
  • stackoverflow.com/questions/26371083/… Commented Mar 23, 2017 at 11:53
  • Thanks :) will look into it. Commented Mar 23, 2017 at 11:54

1 Answer 1

1

You can use slice method before map.

{this.state.products.slice(0, 4).map(...)}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Exactly what I needed.

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.