在JavaScript对象属性的百分比计算的变化 [英] Calculating change in percent of the properties in JavaScript objects

查看:116
本文介绍了在JavaScript对象属性的百分比计算的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算的数据范围的百分比变化由它的前范围。

I would like to calculate the percent change for a range of data by it's prior range.

下面就是我工作的一个例子:

Here's an example of what I'm working on:

下面是对象的数组,我需要计算 displayAd_imp videoAd_imp 百分比变化, tv_impbased 基于选择的属性 WeekNo 系列。

Below is an array of objects that I need to calculate a percent change for displayAd_imp, videoAd_imp, and tv_impbased properties based on a selection of WeekNo range.

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 工作的例子。

In the method below I'm trying to achieve this task by first filtering the data with start and end WeekNo, then calculating the sum of that data. Here's a jsfiddle working example.

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);
}

我在与被越来越previous范围的总和,然后电流/ previous计算该期间的百分比变化困难的部分 - 1。因此,例如,在上面的jsfiddle我们叫 CalcWeekRange(objArr,32,33);

displayAd_imp 的百分比变化的逻辑是:

The percent change logic for displayAd_impwould be:

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

Current Weeks(32,33): 1400+1500 = 2900 (We have this part in sumArr)

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

Previous Weeks(30,31): 1600+1500 = 3100

%变化:(2900 / 3100-1)* 100 = -6.45 (注:负是允许的)

%Change: (2900/3100-1)*100 = -6.45 (Note: negative is allowed)

以上会发生的性质其余为好。

The above would happen for the rest of the properties as well.

希望的问题和例子是明确的,感谢您的时间!

Hopefully the question and examples are clear, thanks for your time!

推荐答案

您的意思是这样的,除了你的上述对象数组和code?

You mean like this in addition to your above object array and code?

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

更新:如果你想获得根据实际本周的结果,这可以通过以下调整上述code来完成:

Update: In case you want to get the results based on the actual current week, this can be done with following adjustment to above code:

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周副本),所以小提琴下周将停止工作:)

I've updated the fiddle and added an object for the actual current week number 34 (just a copy of week 33 for testing), so the fiddle will stop working next week :)

更新演示

这篇关于在JavaScript对象属性的百分比计算的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆