Scenario:
If we want to check whether any particular values are present in an array of objects and to do some actions according to that. Then we can use the following code:
Here the data is from the pricing schedule which is basically an array of jects so here we are finding the price of the item having a particular quantity selected by the customer.
So we have the quantity with us and find the price corresponding to the quantity.
Code snippet:
//Here in the code code we are checking minqunatity = 300 is present or not and getting corresponfing price.
const data = [
{
"minimumquantity": 300,
"maximumquantity": 600,
"price": 0.45,
"price_formatted": "$0.45",
"index": 0
},
// Rest of the data...
];
let priceForMinimumQuantity300 = null;
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (item.minimumquantity === 300) {
priceForMinimumQuantity300 = item.price;
break;
}
}
if (priceForMinimumQuantity300) {
console.log("Price for minimum quantity of 300:", priceForMinimumQuantity300);
} else {
console.log("No object has a minimum quantity of 300.");
}