获取一个在0和1之间的加权值,基于0为最小值,1为最大值。

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

Get a weighted value between 0 and 1 based on zero being min value and 1 max value

问题

让我们假设我有一组数字:Set(6) { 0, 2.58, 2.74, 2.75, 4.12, 5.5 }。它们的总和为17.69

我知道我可以通过将每个数字除以总和来获得介于0和1之间的加权/百分比值,这将产生以下结果:

{
  '0': 0,
  '2.58': 0.14584511023176935,
  '2.74': 0.15488976823063877,
  '2.75': 0.1554550593555681,
  '4.12': 0.2328999434708875,
  '5.5': 0.3109101187111362
}

然而,我正在寻找的结果更像是这样的:

{
  '0': 0,
  '2.58': 约为0.5,
  '2.74': 稍微超过0.5,
  '2.75': 稍微超过0.5,
  '4.12': 约为0.8,
  '5.5': 1
}

因此,数字越接近集合中的最大数字,它就越接近1。

我确信我在这里漏掉了一些简单的东西。

为了提供背景信息,我需要能够在包含这些数字的方框周围显示一个淡化的颜色,因此我正在寻找一个介于0和1之间的值,以便我可以使用CSS将其分配给不透明度。

英文:

Let's set I have a set of numbers: Set(6) { 0, 2.58, 2.74, 2.75, 4.12, 5.5 }. These sum to 17.69.

I know I can get a weighted / percentage based value between 0 and 1 by dividing each number by the total, which produces:

{
  '0': 0,
  '2.58': 0.14584511023176935,
  '2.74': 0.15488976823063877,
  '2.75': 0.1554550593555681,
  '4.12': 0.2328999434708875,
  '5.5': 0.3109101187111362
}

The result I am looking for though, is more like this:

{
  '0': 0,
  '2.58': around 0.5,
  '2.74': just over 0.5,
  '2.75': just over 0.5,
  '4.12': around 0.8,
  '5.5': 1
}

So the closer a number is to the higher number in the set, the closer to 1 it is.

I'm sure I'm missing something simple here.

For context, I need to be able to show a faded color around the box containing these numbers so I'm looking for a value between 0 and 1 so that I can assign that to the opacity using CSS.

答案1

得分: 1

mySet = new Set([0, 2.58, 2.74, 2.75, 4.12, 5.5]);
myArray = Array.from(mySet);
max = myArray.reduce((prev, curr) => {
  if (prev === null) {
    return curr;
  } else if (curr > prev) {
    return curr;
  } else {
    return prev;
  }
}, null);

resultAsArray = myArray.map(item => item / max);

resultsAsObject = myArray.reduce((prev, curr, i) => {
  return Object.assign(prev, { [curr]: resultAsArray[i] });
}, {})

如果你想将类似 0.4982734923847 的数字处理为较短的形式,
你可以这样做,例如:{[curr]: resultAsArray[i].toFixed(2)}

英文:
mySet = new Set([0, 2.58, 2.74, 2.75, 4.12, 5.5]);
myArray = Array.from(mySet);
max = myArray.reduce((prev, curr) => {
  if (prev === null) {
    return curr;
  } else if (curr > prev) {
    return curr;
  } else {
    return prev;
  }
}, null);

resultAsArray = myArray.map(item => item/max);

resultsAsObject = myArray.reduce((prev, curr, i) => {
  return Object.assign(prev, {[curr]: resultAsArray[i]});
}, {})

If you want to handle numbers like 0.4982734923847 as shorter things,
you can do, for example, {[curr]: resultAsArray[i].toFixed(2)}

答案2

得分: 0

对于数组中的每个元素:

  • 减去输入数组的最小值
  • 除以(输入数组的最大值减去最小值)
  • 如果需要,四舍五入。

例如,输入:[7, 13, 17, 22]

  • 减去7得:[0, 6, 10, 15]
  • 除以15得:[0, 6/15, 10/15, 15/15] = [0, 0.4, 0.666667, 1]
英文:

For every element in your array:

  • subtract min(input array)
  • divide by (max(input array) - min(input array))
  • round if you like.

E.g., inputs: [7,13, 17, 22]

  • subtraction by 7 yields: [0, 6, 10, 15]
  • division by 15 yields: [0, 6/15, 10/15, 15/15] = [0, 0.4, 0.666667, 1]

huangapple
  • 本文由 发表于 2023年6月5日 18:57:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405758.html
匿名

发表评论

匿名网友

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

确定