例如 [1,1] --> [50,50]
和 [1,2] --> [33.33, 66.67]
这是我的代码:
let total = getTotal(arrayOfNum);
let remainder = 100 - total;
arrayOfNum.map(n => {
let newValue = n + ( (remainder / total ) * n);
return newValue || 0;
});
当数组中的所有项目均为 0
时,此代码无法正确汇总
[0,0,0] --> [0,0,0]
虽然我期待类似 [0,0,0] --> [33.33, 33.33, 33.33]
最佳答案
你能看到这个解决方案吗?
const t = [0,2];
const total = arrayOfNum.reduce((r,n)=>r+n);
arrayOfNum
.map(n=> n/total)
.map(n=> !isNaN(n) ? n * 100 : 1/arrayOfNum.length * 100 )
关于javascript - 如何对一组数字求和,使数组的总和为 100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49730110/