Skip to content
Advertisement

React app how to show only limited number of array items

I have data.js file where information about t-shirts, hoodies, cases and etc. is contained. I have 5 objects in each array. If I call each array on my HomeScreen.js it shows all the objects that array contains.

How do I make each array show specific number of objects at a certain page? For example in the Home screen it should show 2, but on another page it should show all 5.

Here is my table that calls to data.js arrays:

JavaScript

Advertisement

Answer

You can try using .slice(), which extracts a portion of the array.

For example, for hoodies, you can do something like:

JavaScript

If you want to show only 2 items in the homepage, you can have a conditional statement in the body of the component (before the return statement for the jsx).

For example, you can say..

JavaScript

Is that helpful?

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement