获取 JavaScript 中数组对象的匹配范围项

huangapple go评论75阅读模式
英文:

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:&quot;Tier 1&quot;}

Expeceted Output
{id: 3, discount_percent:10, quantity:10, title:&quot;Tier 3&quot;}

let tiers = [
   {id: 1, discount_percent:0, quantity:6, title:&quot;Tier 1&quot;},
   {id: 2, discount_percent:5, quantity:8, title:&quot;Tier 2&quot;},
   {id: 3, discount_percent:10, quantity:10, title:&quot;Tier 3&quot;},
   {id: 4, discount_percent:12, quantity:12, title:&quot;Tier 4&quot;},
   {id: 5, discount_percent:14, quantity:14, title:&quot;Tier 5&quot;},
   {id: 6, discount_percent:20, quantity:16, title:&quot;Tier 6&quot;},
   {id: 7, discount_percent:40, quantity:18, title:&quot;Tier 7&quot;},
   {id: 8, discount_percent:50, quantity:50, title:&quot;Tier 8&quot;},
]
function calculateDiscount(){
   const ordersQuanity = 10;
   const tier = tiers.find((_tier) =&gt; _tier.quantity &lt;= ordersQuanity);
   ...
}

答案1

得分: 1

对于具有discount_percentquantity的多个对象的一般情况,.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_percents and quantitys, .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:&quot;Tier 1&quot;},
   {id: 2, discount_percent:5, quantity:8, title:&quot;Tier 2&quot;},
   {id: 3, discount_percent:10, quantity:10, title:&quot;Tier 3&quot;},
   {id: 4, discount_percent:12, quantity:12, title:&quot;Tier 4&quot;},
]
function calculateDiscount(){
   const ordersQuanity = 10;
   const bestTier = tiers.reduce((a, tier) =&gt; (
     tier.quantity &lt;= ordersQuanity &amp;&amp; (!a || tier.discount_percent &gt; 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&#39;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:&quot;Tier 1&quot;},
   {id: 2, discount_percent:5, quantity:8, title:&quot;Tier 2&quot;},
   {id: 3, discount_percent:10, quantity:10, title:&quot;Tier 3&quot;},
   {id: 4, discount_percent:12, quantity:12, title:&quot;Tier 4&quot;},
];
const tiersReversed = [...tiers].reverse();
function calculateDiscount(){
   const ordersQuanity = 10;
   const tier = tiersReversed
     .find((_tier) =&gt; _tier.quantity &lt;= ordersQuanity)
      || tiers[0]; // alternate with the first element of the array
   // if you want to default to that tier even if the quantity isn&#39;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:&quot;Tier 1&quot;},
   {id: 2, discount_percent:5, quantity:8, title:&quot;Tier 2&quot;},
   {id: 3, discount_percent:10, quantity:10, title:&quot;Tier 3&quot;},
   {id: 4, discount_percent:12, quantity:12, title:&quot;Tier 4&quot;},
   {id: 5, discount_percent:14, quantity:14, title:&quot;Tier 5&quot;},
   {id: 6, discount_percent:20, quantity:16, title:&quot;Tier 6&quot;},
   {id: 7, discount_percent:40, quantity:18, title:&quot;Tier 7&quot;},
   {id: 8, discount_percent:50, quantity:50, title:&quot;Tier 8&quot;},
]
function calculateDiscount(ordersQuanity){
   const bestTier = tiers.reduce((a, tier) =&gt; (
     tier.quantity &lt;= ordersQuanity &amp;&amp; (!a || tier.discount_percent &gt; 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&#39;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))

huangapple
  • 本文由 发表于 2023年1月6日 14:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定