简体   繁体   English

如何根据一个元素显示正确的计算值?

[英]How to show the correct computed value according an element?

I have this "roi calculator" where the user can range some labels. 我有这个“ roi计算器”,用户可以在其中排列一些标签。 And, I have one range called "onlineRevenue". 而且,我有一个范围叫做“ onlineRevenue”。 The idea is offer the best plan according the onlineRevenue number. 这个想法是根据在线收入数字提供最佳计划。

But, to do this, I have limitations : 但是,为此,我有一些局限性

  • If the onlineRevenue is less than 100 000, I offer the 'essentialYear'; 如果onlineRevenue少于100000,我提供“ essentialYear”;
  • If the onlineRevenue is between 100 000 and 300 000, I offer the 'accelerateYear'; 如果在线收入在10万到30万之间,则我提供“ celebrateYear”;
  • If the onlineRevenue is between 300 000 and 500 000, I offer the 'Ultimate'; 如果onlineRevenue在30万到50万之间,我提供“终极”;

How I can add this limitations using vue to the variable "subscriptionFee" understand the rules above? 我如何使用vue对变量“ subscriptionFee”添加此限制,以了解上述规则?

For example, someone suggest to use the direct numbers, without the data, like that 例如,有人建议使用无数据的直接数字,像那样

          subscriptionFee: function() {
          var feesuggest = this.onlineRevenue <=100000;
          return this.essentialYear;
        },

But the idea is generate a variable where the if the value is X, show that. 但是想法是生成一个变量,如果值是X,则表明该变量。 And with this code above, I can't do comparison between values. 有了上面的这段代码,我无法在值之间进行比较。

I create a jsfiddle to work with the code, to be more easier to understand my question. 我创建了一个jsfiddle来处理代码,以便更轻松地理解我的问题。

How about a solution like so: 这样的解决方案怎么样:

const tiers = [
    { name: 'essential', min: 0, max: 100000 },
    { name: 'accelerate', min: 100000, max: 300000 },
    { name: 'ultimate', min: 300000, max: 500000 }
];

const subscriptionFee = (amount) => {
  const tierMatch = (tier) => amount >= tier.min && amount < tier.max;
  const reducer = (tierFound, tier) => tierMatch(tier) ? tier : tierFound;
  return tiers.reduce(reducer, false);
};

subscriptionFee(500);

You start with an array of tiers over which you iterate and reduce to a single item. 您从一系列的层开始,在这些层上进行迭代并缩减为单个项目。 You still need to consider bigger numbers, because 500001 will give you false as a result. 您仍然需要考虑更大的数字,因为500001会给您带来错误。

subscriptionFee: function() {
  const x = this.onlineRevenue
  switch (true) {
    case (x <= 100000):
      return this.essentialYear
      break;
    case (x > 100000 && x < 300000):
      return this.accelerateYear
      break;
    default:
      // anything over 300000
      return this.ultimate
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM