I have one array which is named as tiers and one quantity variable. Now I am trying to get the matched range value from my tier array. Based on the matched result I want to calculate the discount. I tried to with find method but it gives me an unexpected ouput.
Actual output
{id: 1, discount_percent:0, quantity:6, title:"Tier 1"}
Expeceted Output
{id: 3, discount_percent:10, quantity:10, title:"Tier 3"}
let tiers = [
{id: 1, discount_percent:0, quantity:6, title:"Tier 1"},
{id: 2, discount_percent:5, quantity:8, title:"Tier 2"},
{id: 3, discount_percent:10, quantity:10, title:"Tier 3"},
{id: 4, discount_percent:12, quantity:12, title:"Tier 4"},
{id: 5, discount_percent:14, quantity:14, title:"Tier 5"},
{id: 6, discount_percent:20, quantity:16, title:"Tier 6"},
{id: 7, discount_percent:40, quantity:18, title:"Tier 7"},
{id: 8, discount_percent:50, quantity:50, title:"Tier 8"},
]
function calculateDiscount(){
const ordersQuanity = 10;
const tier = tiers.find((_tier) => _tier.quantity <= ordersQuanity);
...
}
_tier.quantity <= ordersQuanitythis should be_tier.quantity >= ordersQuanityYou need to find the first object where quantity is greater than or equal to given threshold