简体   繁体   English

无法从服务器格式化此JSON数据

[英]Having trouble formatting this JSON data from the server

I'm creating a section of a site in Angular JS that displays some reporting data from the server. 我正在Angular JS中创建网站的一部分,该部分显示来自服务器的一些报告数据。 Since I'm displaying it in a table I need it in a format that can easily be displayed with ng-repeat. 由于我将其显示在表格中,因此我需要它可以使用ng-repeat轻松显示的格式。

I therefore need to translate JSON data from the server into a JavaScript object. 因此,我需要将JSON数据从服务器转换为JavaScript对象。 I've got the target format of the data I want, an object that uses the names of the 'Donor Programs' as keys, and as their values an array of objects that each correspond to a month. 我已经有了想要的数据的目标格式,一个使用“捐赠程序”的名称作为键的对象,以及每个对应一个月的对象数组作为其值。 Each of the objects has the name of the month, the amount of people who registered on the site, the amount of those people who donated, and the total amount donated. 每个对象均具有月份名称,在网站上注册的人数,捐赠的人数以及捐赠的总金额。

I'm just having a really hard time iterating through the JSON and getting it to translate to that format. 我只是很难遍历JSON并将其转换为该格式。 Can someone have a look? 可以看看吗? I've included both the server data I'm getting as well as how I'm trying to format it. 我已经包括了我要获取的服务器数据以及如何对其进行格式化。 Thanks. 谢谢。

Server Data: 服务器数据:

$scope.serverData = {
"registration_by_month": [
    {
        "month": "November 2015",
        "registration_program": "Donor Program 1",
        "count": 3
    },
    {
        "month": "November 2015",
        "registration_program": "Donor Program 2",
        "count": 4
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 1",
        "count": 5
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 2",
        "count": 6
    }
],

"donors_by_month": [
    {
        "month": "November 2015",
        "registration_program": "Donor Program 1",
        "count": 2
    },
    {
        "month": "November 2015",
        "registration_program": "Donor Program 2",
        "count": 1
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 1",
        "count": 2
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 2",
        "count": 1
    }
],

"donated_amount_by_month": [
    {
        "month": "November 2015",
        "registration_program": "Donor Program 1",
        "amount": 100
    },
    {
        "month": "November 2015",
        "registration_program": "Donor Program 2",
        "amount": 200
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 1",
        "amount": 50
    },
    {
        "month": "December 2015",
        "registration_program": "Donor Program 2",
        "amount": 40
    }
]

}; };

Target Data: 目标数据:

$scope.targetData = {
"Donor Program 1": [{month:"November 2015", registered:3, donors:2, donated_amount:100},{month:"December 2015", registered:4, donors:1, donated_amount:200}],
"Donor Program 2:": [{month:"November 2015", registered:5, donors:2, donated_amount:50},{month:"December 2015",registered:6, donors:1, donated_amount:40}]

}; };

Try like this 这样尝试

 var temp = {};
 data.registration_by_month.forEach(function (x) {
     if (!temp[x.registration_program]) {
         temp[x.registration_program] = [];
     }
     var dtmon = data.donors_by_month.find(function (y) {
         return y.registration_program == x.registration_program;
     });
     var dnmon = data.donated_amount_by_month.find(function (y) {
         return y.registration_program == x.registration_program;
     });
     temp[x.registration_program].push({
             month: x.month,
             registered: x.count,
             donors: dtmon ? dtmon.count : 0,
             donated_amount: dnmon ? dnmon.amount : 0
     });

 });
console.log(temp);

JSFIDDLE 的jsfiddle

EDIT 编辑

.find is not supported in IE and opera. IE和Opera中不支持.find

You can try .filter instead 您可以尝试使用.filter代替

Like this 像这样

 var temp = {};
 data.registration_by_month.forEach(function (x) {
     if (!temp[x.registration_program]) {
         temp[x.registration_program] = [];
     }
     var dtmon = data.donors_by_month.filter(function (y) {
         return y.registration_program == x.registration_program;
     });
     var dnmon = data.donated_amount_by_month.filter(function (y) {
         return y.registration_program == x.registration_program;
     });
     temp[x.registration_program].push({
             month: x.month,
             registered: x.count,
             donors: dtmon.length > 0 ? dtmon[0].count : 0,
             donated_amount: dnmon.length > 0 ? dnmon[0].amount : 0
     });

 });

JSFIDDLE 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM