计算JavaScript对象中属性百分比的变化

时间:2014-08-21 23:08:10

标签: javascript jquery arrays object

我想根据它的先前范围来计算一系列数据的变化百分比。

以下是我工作的一个例子:

下面是一系列对象,我需要根据displayAd_imp范围的选择来计算videoAd_imptv_impbasedWeekNo属性的百分比变化。< / p>

var objArr = [{

    "Title": "July 13 - July 19 2014",
        "displayAd_imp": "3,500",
        "videoAd_imp": "1.5",
        "tv_imp": "0.52",
        "Date": "2014-07-17T00:00:00.000Z",
        "WeekNo": 29
}, {

    "Title": "July 20 - July 26 2014",
        "displayAd_imp": "1,600",
        "videoAd_imp": "2.55",
        "tv_imp": "0.052",
        "Date": "2014-07-24T00:00:00.000Z",
        "WeekNo": 30
}, {

    "Title": "July 27 - Aug 2 2014",
        "displayAd_imp": "1,500",
        "videoAd_imp": "2.1",
        "tv_imp": "0.122",
        "Date": "2014-07-31T00:00:00.000Z",
        "WeekNo": 31
}, {

    "Title": "Aug 3 - Aug 9 2014",
        "displayAd_imp": "1,500",
        "videoAd_imp": "1.99",
        "tv_imp": "0.254",
        "Date": "2014-08-07T00:00:00.000Z",
        "WeekNo": 32
}, {

    "Title": "Aug 10 - Aug 17 2014",
        "displayAd_imp": "1,400",
        "videoAd_imp": "2.0",
        "tv_imp": ".235",
        "Date": "2014-08-14T00:00:00.000Z",
        "WeekNo": 33
}];

在下面的方法中,我尝试通过首先使用开始和结束WeekNo过滤数据来实现此任务,然后计算该数据的总和。这是一个jsfiddle工作示例。

function CalcWeekRange(data,begin,end){
    //console.log(data,begin, end);
    var newArr = data.filter(function(item){
        return (item.WeekNo >= begin && item.WeekNo <= end);
    });

    var sumArr = [newArr.reduce(function (acc, x) {
        Object.keys(acc).forEach(function (k) {
        acc[k] += Number(x[k]);
                });
                return acc;
            }, {
                displayAd_imp: 0,
                videoAd_imp: 0,
                tv_imp: 0,
            })];
            console.log(sumArr);
}

我遇到困难的部分是通过获得先前范围和当前/之前的总和来计算该时期的百分比变化 - 例如,在上面的jsfiddle中我们称为{{1} }。

CalcWeekRange(objArr,32,33);的百分比变化逻辑是:

当前周(32,33):1400 + 1500 = 2900(我们在displayAd_imp中有这一部分)

前几周(30,31):1600 + 1500 = 3100

%变化:(2900 / 3100-1)* 100 = -6.45 (注:允许否定)

上述其他属性也会发生。

希望问题和例子很清楚,谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

除了上面的对象数组和代码之外,你的意思是这样吗?

var jCurrent = CalcWeekRange(objArr,32,33);
var jPrevious = CalcWeekRange(objArr,30,31);
var jCurrentDisplay = jCurrent[0].displayAd_imp;
var jPreviousDisplay = jPrevious[0].displayAd_imp;
var result = (jCurrentDisplay/jPreviousDisplay-1)*100;
console.log("result ", result);

在此调整您的jsFiddle:jsFiddle

更新:如果您想根据实际当前周获得结果,可以通过以下调整以上代码来完成:

Date.prototype.getWeek = function() {
 var firstday = new Date(this.getFullYear(),0,1);
 var today = new Date(this.getFullYear(),this.getMonth(),this.getDate());
 var dayOfYear = ((today - firstday + 86400000)/86400000);
 return Math.ceil(dayOfYear/7)
};

var today = new Date();
var currentWeekNumber = today.getWeek();

var jCurrentDynamic = CalcWeekRange(objArr,currentWeekNumber-1,currentWeekNumber);
var jPreviousDynamic = CalcWeekRange(objArr,currentWeekNumber -3,currentWeekNumber -2);

var jCurrentDisplayDynamic = jCurrentDynamic[0].displayAd_imp;
var jPreviousDisplayDynamic = jPreviousDynamic[0].displayAd_imp;
var resultDynamic = (jCurrentDisplayDynamic/jPreviousDisplayDynamic-1)*100;
console.log("resultDynamic ", resultDynamic);

我已经更新了小提琴并添加了一个实际当前周数为34的对象(只是第33周的副本用于测试),因此小提琴将在下周停止工作:)

Updated Demo