I’m working with a large JSON file that looks like this:
{
"name": "Superproduct",
"description": "Enjoy this amazing product.",
"brand": "ACME",
"categories": [
"Ball",
"Soccer Ball",
"Beach Ball"
],
"type": "Online product",
"price": 50,
"price_range": "50 - 100",
"image": "someImageURL",
"url": "SomeProductURL",
"free_shipping": true,
"popularity": 10000,
"rating": 2,
"objectID": "1234"
}
I am trying to access every object with the Ball category, so that I can add a discount to that specific item. I realized that every object can have multiple variations of the word Ball in the category array.
Is there a way I can target the word ball in the array, so that I can add that to an array and apply the discount to every product with said criteria?
This is what I have so far, but I’m not sure if this is the best way, or if there’s a better way to accomplish what I’m trying to do:
async function setDiscount() {
let discountedRate = 0.5;
fetch('products.json')
.then(res => res.json())
.then(data => {for (let i = 0; i < data.length; i++) {
if (data[i].categories[i] == "Ball") {
data[i].price -= (data[i].price * discountedRate);
}
}});
}
setDiscount();
P.S.: I’m a newbie.