lundi 7 décembre 2020

Randomly choose 3 non repeating numbers from array length

Currently I have an array with a multitude of objects in them. Currently there are 21, although this could increase or decrease at any point.

To loop through the array and generate the required content I am using the following code:

for (let i = 0; i < projects.length; i++) { 
    let imagesString = projects[i].images.reduce((acc,image,ind)=>{
        if (ind==0) return acc;
    return acc +'<a href="' + projects[i].imagelocation + image + '" data-fancybox="' + projects[i].fancybox + '" data-caption=" ' + projects[i].description + '"></a>'},"");

    content += '<div class="galleryitem col-lg-4 col-md-4 col-sm-6 ' + projects[i].category + '"><a href="' + projects[i].imagelocation + projects[i].images[0] + '" data-fancybox="' + projects[i].fancybox + '" data-caption=" ' + projects[i].description + '"><div class="h_gallery_item"><div class="g_img_item"><i class="fas fa-expand expand"></i><img class="img-fluid" src="' + projects[i].imagelocation + projects[i].thumbnail + '" alt="' + projects[i].name + ' - ' + projects[i].subheading + '"></div><div class="g_item_text"><h4>' + projects[i].name + '</h4><p>' + projects[i].subheading + '</p></div></div></a>'+imagesString+'</div>';    
}

In a different section of the web app, I would like to have it select 3 random non-repeating objects from this array and display them, by running through the loop, but instead of let i = 0; i < projects.length; i++ I need I to be 3 random non-repeating numbers from projects.length.




Aucun commentaire:

Enregistrer un commentaire