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

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

此功能的作用是,对于每个优先级,它将重新平衡相等部分的百分比(参与)。问题发生在奇怪的情况下,例如当有 3 个优先级(Prioridad)时,您必须除以 100 / 3,结果为 33(和 33 + 33 + 33 = 99 )

我应该在我的代码中添加什么,以便当除法结果不是 100 时,将缺少的数字添加到最后一个位置,完成 100。

示例问题 1:Example gif:想法是您在 gif 中看到的最后 33 个自动转换为 34.Tthis 是我目前的代码:

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;
      })
  }

问题 2:在前面的 gif 中,您可以看到当您按下按钮时,所有优先级 (Prioridad) 都已重新平衡,我希望只针对百分比为 0 的那些(参与)

我很确定这两个问题都可以通过在上面的函数中添加几行代码来解决,但是我缺乏经验不知道该怎么做

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

我修正了你的算法。真的只是需要一个稍微不同的方法。我将其更改为使用 Math.floor() 进行参与,然后获取使值变为 100% 所需的差异(100% 长度)并将其添加到最后一个值。这是更新的 stackblitz。

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

PS:永远不要在模板中进行方法调用,因为每次运行更改检测时都会增加此添加的开销;而是使用纯管道或计算它们在数据更改时显示的值。有一个被调用的十进制格式化方法被我删除了。