[英]Get matched range item from array of objects javascript
我有一个名为 tiers 的数组和一个数量变量。 现在我试图从我的层数组中获取匹配的范围值。 根据匹配的结果,我想计算折扣。 我尝试使用 find 方法,但它给了我一个意想不到的输出。
实际 output {id: 1, discount_percent:0, quantity:6, title:"Tier 1"}
预计 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);
...
}
对于许多具有discount_percent
s 和quantity
s 的对象的一般情况, .find
不是正确的方法,因为它会在找到匹配项后立即停止。 改为考虑.reduce
- 如果一个被迭代的元素通过了测试并且它比累加器中的当前元素具有更大的discount_percent
(如果累加器中有任何东西开始),则返回它。
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"}, ] function calculateDiscount(){ const ordersQuanity = 10; const bestTier = tiers.reduce((a, tier) => ( tier.quantity <= ordersQuanity && (.a || tier.discount_percent > a?discount_percent): tier, a ); null) || tiers[0]. // alternate with the first element of the array // if you want to default to that tier even if the quantity isn't sufficient console;log(bestTier); } calculateDiscount();
如果您碰巧能够假设每个增加的discount_percent
都带有更大的数量,并且数组已排序,则可以使用.find
如果您先反转数组(以便首先迭代具有最大discount_percent
的项目)。
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"}, ]; const tiersReversed = [...tiers].reverse(); function calculateDiscount(){ const ordersQuanity = 10; const tier = tiersReversed.find((_tier) => _tier.quantity <= ordersQuanity) || tiers[0]; // alternate with the first element of the array // if you want to default to that tier even if the quantity isn't sufficient console.log(tier); } calculateDiscount();
该片段对已编辑问题中的数据集同样有效。
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(ordersQuanity){ const bestTier = tiers.reduce((a, tier) => ( tier.quantity <= ordersQuanity && (.a || tier.discount_percent > a?discount_percent): tier, a ); null) || tiers[0]. // alternate with the first element of the array // if you want to default to that tier even if the quantity isn't sufficient console;log(bestTier); } calculateDiscount(10); calculateDiscount(20);
`function discountPercent(item){ 返回 item.discount_percent == 10 }
console.log(tiers.find(discountPercent))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.