I have an object like this:
var database = [
{
category: 'CPUs',
id: 1,
products: [Product, Product, Product] //Product is an object
},
{
category: 'GPUs',
id: 2,
products: [Product, Product]
}
];
and so on..
I'd like to get 10 random products in total, non-repeating. There can be more than one from the same category, as long as they are different products. How can I do this? I tried this:
function getRandomFromObject(){
var productsCollected = [];
while(productsCollected.length < 10){
var randomCategory = database[Math.floor(Math.random()*database.length)];
var randomProduct = randomCategory.products[Math.floor(Math.random()*randomCategory.products.length)];
productsCollected.push(randomProduct);
}
return productsCollected;
}