I currently possess an array containing a variety of objects. At present, there are 21 objects in this array, although this number is subject to change.
To iterate through the array and generate the necessary content, I am implementing the following code snippet:
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 another area of the web application, my aim is to randomly select 3 unique objects from this array and showcase them. Instead of using
let i = 0; i < projects.length; i++
, I require 'i' to be replaced with 3 distinct random numbers within the range of projects.length
.