Shorting the Hard Code

Hello everyone,

I am running a store which has more than 9000 products.

I want to show some randomly selected items on the main page. I managed it by hard-coding:


There are 12 images and 12 texts in the code. And I am assigning them separately with a for loop.

Is there any way to do this using a repeater?

My code:

let storeItems ;

let images = [ “#image2” , “#image3” , “#image4” , “#image5” , “#image6” , “#image7” , “#image8” , “#image9” , “#image10” , “#image11” , “#image12” , “#image13” ];
let texts = [ “#text32” , “#text33” , “#text34” , “#text35” , “#text36” , “#text37” , “#text38” , “#text39” , “#text40” , “#text41” , “#text42” , “#text43” ];

$w . onReady ( function () {
$w ( “#dataset1” ). onReady (()=> {
for ( let i = 0 ; i < 12 ; i ++){
$w ( “#dataset1” ). getItems ( getRandomIndex ( 0 , $w ( “#dataset1” ). getTotalCount ()- 1 ), 1 )
. then ( ( result ) => {
storeItems = result . items ;
$w ( images [ i ]). src = storeItems [ 0 ]. mainMedia ;
$w ( images [ i ]). link = storeItems [ 0 ]. productPageUrl ;
$w ( texts [ i ]). text = storeItems [ 0 ]. name ;
})
}
});
});

//random array index
function getRandomIndex ( min , max ) {
return Math . round ( Math . random () * ( max - min ) + min );
}

Thank you.