Im using a each loop and pulling from a JSON database. Currently this loop randomizes the JSON and outputs a random item in the DIV. Im trying to figure out how I can add to this and have it pick a random item from a category instead of everything.
Below is my code:
function similarProduct() {
$.each(json, function(i,item){
similarProduct = json[Math.floor(Math.random()*json.length)];
similarProduct += '<div>' + '<img src="' + similarProduct.imageURL + '">' + '<h3>' + similarProduct.itemName + '</h3>' + '</div>';
});
$('#productSimilar').append(similarProduct);
}
My JSON format looks like this:
[
{
"itemName":"Organic Tomatoes",
"imageURL":"",
"itemCategory":"Tomatoes"
},
{
"itemName":"Olive Oils",
"imageURL":"",
"itemCategory":"Olive Oil"
}
]