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
x
45
45
1
<span className="productstitle">Featured T-Shirts</span>
2
<a href={`/category/tshirts`} className="browseall">All designs ></a>
3
4
5
<table className="maintable">
6
{data.tshirts.map((product) => (
7
<Product key={product._id} product={product}></Product>
8
))}
9
10
<p className='featuredtext'><span className="productstitle">Featured Hoodies</span>
11
<a href={`/category/hoodies`} className="browseall">All designs ></a></p>
12
13
{data.hoodies.map((product) => (
14
<Product key={product._id} product={product}></Product>
15
))}
16
17
<p className='featuredtext'><span className="productstitle">Featured Phone Cases</span>
18
<a href={`/category/cases`} className="browseall">All designs ></a></p>
19
20
{data.cases.map((product) => (
21
<Product key={product._id} product={product}></Product>
22
))}
23
24
<p className='featuredtext'><span className="productstitle">Featured Pins</span>
25
<a href={`/category/stickers`} className="browseall">All designs ></a></p>
26
27
{data.pins.map((product) => (
28
<Product key={product._id} product={product}></Product>
29
))}
30
31
<p className='featuredtext'><span className="productstitle">Featured Posters</span>
32
<a href={`/category/posters`} className="browseall">All designs ></a></p>
33
34
{data.posters.map((product) => (
35
<Product key={product._id} product={product}></Product>
36
))}
37
38
<p className='featuredtext'><span className="productstitle">Featured Mugs</span>
39
<a href={`/category/mugs`} className="browseall">All designs ></a></p>
40
41
{data.mugs.map((product) => (
42
<Product key={product._id} product={product}></Product>
43
))}
44
</table>
45
Advertisement
Answer
You can try using .slice()
, which extracts a portion of the array.
For example, for hoodies, you can do something like:
JavaScript
1
4
1
{data.hoodies.slice(0, 2).map((product) => (
2
<Product key={product._id} product={product}></Product>
3
))}
4
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
1
4
1
if (homepage) {
2
data.hoodies = data.hoodies.slice(0, 2);
3
}
4
Is that helpful?