Adam Adam - 4 months ago 9
JSON Question

jQuery prevent duplicate values when getting random values

I want to be able to display 8 random 'IQ' values from the 'celebrities' array, but on occasions when refreshing the random 'IQ' value match one another.

How do I prevent the duplicate random values?

Any and all advice would be helpful, thank you.

JS

var selectedCelebrities = [];
for (var c = 0; c < 8; c++){
selectedCelebrities[c] = celebrities[Math.floor(Math.random() * celebrities.length)];
}


JSON

var celebrities = [
{
"IQ": 79,
"Name": "NA"
},
{
"IQ": 85,
"Name": "NA"
},
{
"IQ": 86,
"Name": "NA"
},
{
"IQ": 94,
"Name": "NA"
},
{
"IQ": 104,
"Name": "NA"
},
{
"IQ": 119,
"Name": "NA"
},
{
"IQ": 120,
"Name": "NA"
},
{
"IQ": 125,
"Name": "NA"
},
{
"IQ": 130,
"Name": "NA"
},
{
"IQ": 131,
"Name": "NA"
},
{
"IQ": 135,
"Name": "NA"
},
{
"IQ": 137,
"Name": "NA"
},
{
"IQ": 138,
"Name": "NA"
},
{
"IQ": 140,
"Name": "NA"
},
{
"IQ": 141,
"Name": "NA"
},
{
"IQ": 142,
"Name": "NA"
},
{
"IQ": 154,
"Name": "NA"
},
{
"IQ": 155,
"Name": "NA"
},
{
"IQ": 156,
"Name": "NA"
},
{
"IQ": 159,
"Name": "NA"
},
{
"IQ": 160,
"Name": "NA"
},
{
"IQ": 161,
"Name": "NA"
},
{
"IQ": 162,
"Name": "NA"
},
{
"IQ": 163,
"Name": "NA"
},
{
"IQ": 165,
"Name": "NA"
},
{
"IQ": 170,
"Name": "NA"
},
{
"IQ": 175,
"Name": "NA"
},
{
"IQ": 178,
"Name": "NA"
},
{
"IQ": 180,
"Name": "NA"
},
{
"IQ": 190,
"Name": "NA"
},
{
"IQ": 220,
"Name": "NA"
}
];

Answer

You could use splice to remove the element you have selected, so it cannot be selected again. Of course, you should do this on a copy of the original array:

var selectedCelebrities = [];
var copyCelebrities = celebrities.slice();
for (var c = 0; c < 8; c++){
    selectedCelebrities[c] = copyCelebrities.splice(Math.floor(Math.random() * copyCelebrities.length), 1)[0];
}

Snippet with your sample data:

var celebrities = [
{
  "IQ": 79,
  "Name": "NA"
},
{
  "IQ": 85,
  "Name": "NA"
},
{
  "IQ": 86,
  "Name": "NA"
},
{
  "IQ": 94,
  "Name": "NA"
},
{
  "IQ": 104,
  "Name": "NA"
},
{
  "IQ": 119,
  "Name": "NA"
},
{
  "IQ": 120,
  "Name": "NA"
},
{
  "IQ": 125,
  "Name": "NA"
},
{
  "IQ": 130,
  "Name": "NA"
},
{
  "IQ": 131,
  "Name": "NA"
},
{
  "IQ": 135,
  "Name": "NA"
},
{
  "IQ": 137,
  "Name": "NA"
},
{
  "IQ": 138,
  "Name": "NA"
},
{
  "IQ": 140,
  "Name": "NA"
},
{
  "IQ": 141,
  "Name": "NA"
},
{
  "IQ": 142,
  "Name": "NA"
},
{
  "IQ": 154,
  "Name": "NA"
},
{
  "IQ": 155,
  "Name": "NA"
},
{
  "IQ": 156,
  "Name": "NA"
},
{
  "IQ": 159,
  "Name": "NA"
},
{
  "IQ": 160,
  "Name": "NA"
},
{
  "IQ": 161,
  "Name": "NA"
},
{
  "IQ": 162,
  "Name": "NA"
},
{
  "IQ": 163,
  "Name": "NA"
},
{
  "IQ": 165,
  "Name": "NA"
},
{
  "IQ": 170,
  "Name": "NA"
},
{
  "IQ": 175,
  "Name": "NA"
},
{
  "IQ": 178,
  "Name": "NA"
},
{
  "IQ": 180,
  "Name": "NA"
},
{
  "IQ": 190,
  "Name": "NA"
},
{
  "IQ": 220,
  "Name": "NA"
}
];


var selectedCelebrities = [];
var copyCelebrities = celebrities.slice();
for (var c = 0; c < 8; c++){
    selectedCelebrities[c] = copyCelebrities.splice(Math.floor(Math.random() * copyCelebrities.length), 1)[0];
}
console.log(selectedCelebrities);

Comments