I have an array of objects of countries
const countries = [
{
capital: "Kabul",
countryISOCode: "af",
continent: "Asia",
countryFullName: "Afghanistan",
},
{
capital: "Mariehamn",
countryISOCode: "ax",
continent: "Europe",
countryFullName: "Aland Islands",
},
{
capital: "Tirana",
countryISOCode: "al",
continent: "Europe",
countryFullName: "Albania",
},
{
capital: "Algiers",
countryISOCode: "dz",
continent: "Africa",
countryFullName: "Algeria",
},
{
capital: "Pago Pago",
countryISOCode: "as",
continent: "Oceania",
countryFullName: "American Samoa",
},
{
capital: "Andorra la Vella",
countryISOCode: "ad",
continent: "Europe",
countryFullName: "Andorra",
}
]
I want to randomly select an object which I currently do with
const randomCountry = Math.floor(Math.random() * countries.length);
Problem is that there are often duplicates, i.e. the same country gets chosen twice in a row. I want to add that a country is not able to be selected again for x amount of random selections to make sure it does not appear so often. What would be the best approach to make this work?