0

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.

evilgenious448
  • 518
  • 2
  • 11
  • Shuffle `projects`, then take the first three items from that shuffled array. – CBroe Dec 07 '20 at 14:47
  • Hi @CBroe I have not used the shuffle before, are you able to show me how I can do that, or link me to information about how the shuffle function works? – evilgenious448 Dec 07 '20 at 14:55
  • [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – pilchard Dec 07 '20 at 14:59
  • Typing “javascript array shuffle” into a search engine of your choice, or even the search field on top of this site, can lead you to https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array in no time here. – CBroe Dec 07 '20 at 14:59
  • @CBroe got it to work with your shuffle suggestion. First time that I use it, didn't know it was a thing. Thought I had to math.random, but didn't know how to prevent duplicates. Thank you. – evilgenious448 Dec 07 '20 at 15:14

2 Answers2

0

Sooo, this works for me :

let threeProjects = [];
for(let j = 0; j <3; j++) {
    let value = Math.floor(Math.random() * projects.length);
    if (!threeProjects.includes(value)) {
        threeProjects.push(value)
    } else {
      j--
    }
};
Astoo
  • 11
  • 1
  • 2
  • This just “throws the dice” a couple of times, but it does not ensure that these numbers are unique. – CBroe Dec 07 '20 at 15:03
  • yes sorry, i did press add comment without wanting it :D – Astoo Dec 07 '20 at 15:04
  • I'm gonna try what i've done as an edit, because i don't know if it works to be honnest, but let's see :D – Astoo Dec 07 '20 at 15:05
  • So, i've done a little fix to it (like adding a first declaration of an empty array) and it works for me. Feel free to tel me how it works for you and if it's ok for you :) – Astoo Dec 07 '20 at 15:18
  • it wont work if you have less 3 projects tho. Prob gonna do an infinite loop if you do that :D – Astoo Dec 07 '20 at 15:43
0
const arr = []         // Your Object array
const randIndices = [] // Array of 3 random indices between 0 - arr.length
for(let i = 0; i < 3; i++) {
   randIndices.push(Math.floor(Math.random() * arr.length))
}

console.log(randIndices)

EDIT: This does have a large change of repeats with smaller arrays but will improve as the array's length grows.

Faraz
  • 433
  • 3
  • 7
  • This also just “throws the dice” a couple of times, but it does not ensure that these numbers are unique. – CBroe Dec 07 '20 at 15:02
  • Thats true, this will have a large change of repeats with smaller arrays but will improve as the array's length grows. I have added this to the answer. – Faraz Dec 07 '20 at 15:22