在Javascript中转换对象数组

时间:2016-05-18 17:53:09

标签: javascript arrays object format

我正在尝试使用javascript转换对象数组。下面是原始数据的示例以及格式化数组的外观。我已经尝试用一些循环完成这个,如果,else语句但是无法弄明白。必须有一种更有效的方法来完成这样的任务与javascript?我是新工作的JavaScript,所以任何帮助非常感谢,谢谢。

我试过这个......

// get formatted reports array
        $scope.reports = [];
        var num = 1;
        // console.log("reports: ", $scope.reports_data);
        for(var i=0; i<$scope.reports_data.length; i++) {
            var row = {
                "reviewer_1" : "",
                "reviewer_2" : "",
                "reviewer_3" : "",
                "created_at" : ""
            };
            if(num == $scope.reports_data[i].report_id) {
                row.created_at = $scope.reports_data[i].created_at;
                if($scope.reports_data[i].stage == 1 ) {
                    row.reviewer_1 = $scope.reports_data[i].full_name;
                } else if($scope.reports_data[i].stage == 2) {
                    row.reviewer_2 = $scope.reports_data[i].full_name;
                } else {
                    row.reviewer_3 = $scope.reports_data[i].full_name;
                }
            }
            $scope.reports.push(row);
            num += 1;
        }

之前:

    var raw_arr = [
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Dick",
        "report_id" : 1,
        "stage" : 1
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Tom",
        "report_id" : 1,
        "stage" : 2
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Harry",
        "report_id" : 1,
        "stage" : 3
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"John",
        "report_id" : 2,
        "stage" : 1
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Mike",
        "report_id" : 2,
        "stage" : 2
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Charles",
        "report_id" : 2,
        "stage" : 3
    }
];

之后:

 var formatted_arr = [
        {
            "created_at": "2016-05-04T16:45:07.000Z",
            "report_id": 1,
            "reviewer_1": "Dick",
            "reviewer_2": "Tom",
            "reviewer_3": "Harry",
        },
        {
            "created_at": "2016-05-04T16:45:07.000Z",
            "report_id": 2,
            "reviewer_1": "John",
            "reviewer_2": "Mike",
            "reviewer_3": "Charles",
        }
    ];

2 个答案:

答案 0 :(得分:1)

Array.reduce是你的朋友。通常我建议使用id作为键缩减为对象,但由于你的键无论如何都是数字,我们可以直接将它们用作数组的键:

    var raw_arr = [
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Dick",
        "report_id" : 1,
        "stage" : 1
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Tom",
        "report_id" : 1,
        "stage" : 2
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Harry",
        "report_id" : 1,
        "stage" : 3
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"John",
        "report_id" : 2,
        "stage" : 1
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Mike",
        "report_id" : 2,
        "stage" : 2
    },
    {
        "created_at" :"2016-05-04T16:45:07.000Z",
        "full_name" :"Charles",
        "report_id" : 2,
        "stage" : 3
    }
];

var outputArr = raw_arr.reduce(function(p,c) {
    if (!p[c.report_id - 1]) {
        p[c.report_id - 1] = { 
            created_at: c.created_at,
            report_id: c.report_id
        }
    }
    p[c.report_id - 1]["reviewer_" + c.stage] = c.full_name;
    return p;
},[]);

console.log(JSON.stringify(outputArr));

重要的部分在这里:

var outputArr = raw_arr.reduce(function(p,c) {
    if (!p[c.report_id - 1]) {
        p[c.report_id - 1] = { 
            created_at: c.created_at,
            report_id: c.report_id
        }
    }
    p[c.report_id - 1]["reviewer_" + c.stage] = c.full_name;
    return p;
},[]);

答案 1 :(得分:0)

var grouped = lodash.groupBy(raw_array, 'report_id');
lodash.map(grouped, function(collection) {
    var result = collection[0];
    delete result.full_name;
    delete result.stage;
    lodash.map(collection, function(item, i) {
        result['reviewer_' + (i + 1)] = item.full_name;
    });
    return result;
});