猿问

计算累积镜面反射值

假设我有一个像这样的对象数组:


const data = [

  { value: 0.35, name: 'a' },

  { value: 0.12, name: 'b' },

  { value: 0.05, name: 'c' },

  { value: 0.25, name: 'd' },

  { value: 0.23, name: 'e' },

]

总和正好是 1.0。


这是我想要得到的结果:


const data = [

  { value: 0.35, name: 'a', cumulativeValue: 0.35 },

  { value: 0.12, name: 'b', cumulativeValue: 0.47 },

  { value: 0.05, name: 'c', cumulativeValue: 0.52 },

  { value: 0.25, name: 'd', cumulativeValue: 0.48 },

  { value: 0.23, name: 'e', cumulativeValue: 0.23 },

]

逻辑是累加直到总和超过50,一旦超过这个值就从数组末尾开始计算累加值。


这是我的代码,它可以工作,但是有没有更优雅的方法来做到这一点?


const data = [

  { value: 0.35, name: 'a' },

  { value: 0.12, name: 'b' },

  { value: 0.05, name: 'c' },

  { value: 0.25, name: 'd' },

  { value: 0.23, name: 'e' },

]


function computeCumulative(data) {

    const newData = data.reduce((prevWithCumPercentages, datum, i) => {

      const cumulativeParcentage = i === 0 ? 0 : prevWithCumPercentages[i - 1].cumulativePercentageL

      const newDatum = {

        ...datum,

        cumulativePercentageL: datum.value + cumulativeParcentage,

      }

      prevWithCumPercentages.push(newDatum)

      return prevWithCumPercentages

    }, []) 


    const dataReverse = [...data].reverse().reduce((prevWithCumPercentages, datum, i) => {

      const cumulativeParcentage = i === 0 ? 0 : prevWithCumPercentages[i - 1].cumulativePercentageR

      const newDatum = {

        ...datum,

        cumulativePercentageR: datum.value + cumulativeParcentage,

      }

      prevWithCumPercentages.push(newDatum)

      return prevWithCumPercentages

    }, []) 



慕码人2483693
浏览 86回答 2
2回答

慕无忌1623718

不确定是否足够优雅。我不会为此使用函数式编程,简单的循环就足够了。由于边界条件(当条件触发时,它应应用于下一个值),此分配非常棘手。这意味着要么有一些 if 子句,要么只是反向迭代。简单的java代码:double[] values = {0.35, 0.12,0.05,0.25,0.23 };double cumulative = 0;for(int i = values.length-1; i >= 0 ; i--) {    cumulative += values[i];    double c = cumulative < 0.5 ?  cumulative  :1 - cumulative + values[i];    System.out.println("c[" + i + "]=" + c );}生产:c[4]=0.23c[3]=0.48c[2]=0.52c[1]=0.47c[0]=0.35JavaScript 版本是:const data = [    { value: 0.35, name: 'a' },    { value: 0.12, name: 'b' },    { value: 0.05, name: 'c' },    { value: 0.25, name: 'd' },    { value: 0.23, name: 'e' },];for(let i = data.length - 1, cumulated = 0; i >= 0; i--) {    cumulated += data[i].value;    data[i].cumulated = cumulated < 0.5 ? cumulated : 1 - cumulated + data[i].value;}console.log(data);

哆啦的时光机

您可以对总和进行闭包,并在加/减值的前半部分或后半部分设置一个标志。对于最后一个对象,采用原始值以省略浮点运算错误。const&nbsp; &nbsp; data = [{ value: 0.35, name: 'a' }, { value: 0.12, name: 'b' }, { value: 0.05, name: 'c' }, { value: 0.25, name: 'd' }, { value: 0.23, name: 'e' }],&nbsp; &nbsp; result = data.map(((cumulativeValue, first) => (o, i, { length }) => {&nbsp; &nbsp; &nbsp; &nbsp; if (first) cumulativeValue += o.value;&nbsp; &nbsp; &nbsp; &nbsp; const temp = { ...o, cumulativeValue };&nbsp; &nbsp; &nbsp; &nbsp; if (!first) cumulativeValue -= o.value;&nbsp; &nbsp; &nbsp; &nbsp; if (cumulativeValue > 0.5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cumulativeValue = 1 - cumulativeValue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return i + 1 === length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? { ...o, cumulativeValue: o.value }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : temp;&nbsp; &nbsp; })(0, true))console.log(result).as-console-wrapper { max-height: 100% !important; top: 0; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答