简体   繁体   English

如何使用此代码添加缺失的数字,并且仅在 angular 7 中的变量为 0 时执行此操作

[英]How to add the missing numbers with this code and only doing it when a variable is 0 in angular 7

What this functionality does is that for each priority it will rebalance the percentage (Participacion) in equal parts.此功能的作用是,对于每个优先级,它将重新平衡相等部分的百分比(参与)。 The problem occurs in odd cases, for example when there are 3 priorities(Prioridad), you must divide 100 / 3, which gives a result of 33 (and 33 + 33 + 33 = 99 )问题发生在奇怪的情况下,例如当有 3 个优先级(Prioridad)时,您必须除以 100 / 3,结果为 33(和 33 + 33 + 33 = 99 )

What should I add to my code, so that when the result of the division is not 100, the missing numbers are added to the last position, completing the 100.我应该在我的代码中添加什么,以便当除法结果不是100时,将缺少的数字添加到最后的position,完成100。

Example problem 1: Example gif : The idea is that the last 33 that you see in the gif is automatically transformed into a 34.Tthis is the code i have so far:示例问题 1:示例 gif :想法是您在 gif 中看到的最后一个 33 自动转换为 34。这是我到目前为止的代码:

rebalance(){
    this.beneficiarios = this.beneficiarios.sort((a, b) =>Number(a.Prioridad) - Number(b.Prioridad)).map((val, i, beneficiarios) => {
      const priorityLenght = this.beneficiarios.filter((a) =>
      Number(a.Prioridad) == Number(val.Prioridad)).map((b) =>
      Number(b.Participacion)).length
        const arrObj = {
          ...val,
          Participacion : 100 / priorityLenght
        }
        return arrObj;
      })
  }

Problem 2: In the previous gif you can see that when you press the button, the rebalancing was done for all the priorities (Prioridad), I would like it to be only for those that contain a 0 in percentage (Participacion)问题 2:在前面的 gif 中,您可以看到当您按下按钮时,所有优先级 (Prioridad) 都已重新平衡,我希望只针对百分比为 0 的优先级 (Participacion)

I'm pretty sure that both problems can be solved by adding a couple lines of code to the function I put above, but I lack experience to know what to do我很确定这两个问题都可以通过向上面的 function 添加几行代码来解决,但是我缺乏经验不知道该怎么做

Here a StackBlizz of the proyect: https://stackblitz.com/edit/create-a-basic-angular-component-v8k1hs?file=src/app/example/user.component.ts这里是项目的 StackBlizz: https://stackblitz.com/edit/create-a-basic-angular-component-v8k1hs?file=src/app/example/user.component.ts

I fixed your algorithm.我修正了你的算法。 Really just needed a slightly different approach.真的只是需要一个稍微不同的方法。 I changed it to use Math.floor() for participation and then got the diff (100 % length) needed to make the value 100% and added that to the last value.我将其更改为使用 Math.floor() 进行参与,然后获得使值成为 100% 所需的差异(100% 长度)并将其添加到最后一个值。 Here is the updated stackblitz.这是更新的 stackblitz。

https://stackblitz.com/edit/create-a-basic-angular-component-7vj1rr?file=src/app/example/user.component.html https://stackblitz.com/edit/create-a-basic-angular-component-7vj1rr?file=src/app/example/user.component.html

PS: never make method calls in the template due to the overhead the this add each time change detection runs; PS:永远不要在模板中进行方法调用,因为每次运行更改检测时都会添加 this 的开销; instead use pure pipes or calculate their value for presentation when the data changes.而是使用纯管道或计算它们在数据更改时显示的值。 There was a decimal formatting method being called that I removed.有一个被调用的十进制格式化方法被我删除了。

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

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