英文:
Get matched range item from array of objects javascript
问题
I can provide the translation for the code portion:
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);
// Rest of your code...
}
Please note that I have corrected the code by removing HTML escape codes from the title
properties to make it more readable.
英文:
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);
...
}
答案1
得分: 1
对于具有discount_percent
和quantity
的多个对象的一般情况,.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);
英文:
For the generic situation of a number of objects with discount_percent
s and quantity
s, .find
isn't the right approach because it'll stop as soon as it finds a match. Consider .reduce
instead - if an element being iterated over passes the test and it has a greater discount_percent
than the current element in the accumulator (if there's anything in the accumulator to begin with), return it instead.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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();
<!-- end snippet -->
If you happen to be able to assume that every increased discount_percent
comes with a larger quantity, and the array is sorted, you can use .find
if you reverse the array first (so that the items with the greatest discount_percent
are iterated over first).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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();
<!-- end snippet -->
The snippet works just as well for the dataset in the edited question.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- end snippet -->
答案2
得分: 0
function discountPercent(item){
return item.discount_percent == 10
}
console.log(tiers.find(discountPercent))
英文:
`function discountPercent(item){
return item.discount_percent == 10
}
console.log(tiers.find(discountPercent))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论