循环函数并根据结果创建新数组

时间:2018-06-27 21:08:29

标签: javascript arrays reactjs lodash

这是一个非常繁琐的任务,因此添加了详细的jsfiddle https://jsfiddle.net/tpgx6em7/

到目前为止,..我整理了一些将字符串值转换为数字分数的逻辑。通过这些分数,我根据迄今为止的最近12个月计算了一个百分位值。

我现在需要做的是基于到目前为止的12个月,获得最后11个,最后10个,最后9个,以此类推-如果我要获得最近10个,而本月是3月,我将不包括3月,2月和然后工作10个月。在经过下面的相同功能之后,我将在12个月内得出一个单独的百分位数。

然后我将这些写入新数组。

这是我获得当前月份分数的代码...

    var observations = [{
        gameScore: "R",
        reportDate: "2018-05-09"
      },
      {
        gameScore: "M",
        reportDate: "2018-01-09"
      },
      {
        gameScore: "M+",
        reportDate: "2018-02-09"
      },
  {
        gameScore: "M+",
        reportDate: "2018-01-09"
      },
  {
        gameScore: "M+",
        reportDate: "2018-02-09"
      },
    ];


const SCORES = {
    'R': 2,
    'M': 1,
    'M+': 1.5,
}

//CUTS ARRAY DOWN TO YEAR TO DATE
//MAPS THE SCORES TO THE CORRECT OBJECTS
var calculation = _(observations)
    .filter(observation => {
        return (
            moment(observation.reportDate).year() == moment().subtract('years', 1).year() && moment(observation.reportDate).month() > moment().subtract('years', 1).month()) ||
            (moment(observation.reportDate).year() == moment().year() && moment(observation.reportDate).month() < moment().month());
    })
    .omitBy(x => x.gameScore === "NULL")
    .map(observation => ({ ...observation, value: SCORES[observation.gameScore] }))
    .value();

//FILTERED ARRAY LENGTH *2
const reportpercent = parseInt(calculation.length * 2);

//GETS MATCHING VALUES(R,M,M+) FROM NEW ARRAY AND ADDS UP INDIVIDUAL TOTALS
const count = calculation.reduce((calculation, cur) => {
    calculation[cur.gameScore] = calculation[cur.gameScore] + cur.value || cur.value;
    return calculation;
}, {});

//ADDS UP THE COUNT VALUES (ALL VALUES(R,M,M+) TOGETHER
const sumValues = _.sumBy(_.values(count))

// GETS LATEST %SCORE OVER 12 MONTHS
const total = _.divide(sumValues, reportpercent);

0 个答案:

没有答案
相关问题