如何使用javascript将每个子节点的值总结为JSON中的父节点?

时间:2016-11-09 11:22:48

标签: javascript angularjs json

我从服务器收到一个结构流畅的JSON。哪个在root上有阶段和/或审核集。如果某个阶段确实存在审核集对象数组或另一个阶段审核集包含2个保存数据的属性。

以下是示例JSON

    {
    "id": "chapter",
    "chapterName": "Example Chapter",
    "stages": [
        {
            "id": "stage",
            "stages": [
                {
                    "id": "stage",
                    "stages": [],
                    "reviewSets": [
                        {
                            "id":"reviewset-1",
                            "yettoanswer": 2,
                            "answered": 4
                        }
                    ],
                }
            ],
            "reviewSets": [
                {
                    "id": "reviewset-2",
                    "yettoanswer": 3,
                    "answered": 5
                },
                {
                    "id": "rewviewset-3",
                    "yettoanswer": 6,
                    "answered": 0
                }
            ]
        },
        {
            "id": "stage",            
            "stages": [
                {
                    "id": "stage",
                    "stages": [],
                    "reviewSets": [
                        {
                            "id": "reviewset-4",
                            "yettoanswer": 0,
                            "answered": 8
                        },
                        {
                            "id": "reviewset-5",
                            "yettoanswer": 4,
                            "answered": 4
                        }                        
                    ]
                }
            ],
            "reviewSets": [
                {
                    "id": "reviewset-6",
                    "yettoanswer": 1,
                    "answered": 2
                },
                {
                    "id": "reviewset-7",
                    "yettoanswer": 6,
                    "answered": 4
                }
            ]
        }
    ],
    "reviewSets": [
        {
            "id": "reviewset-8",
            "yettoanswer": 0,
            "answered": 2
        },
        {
            "id": "reviewset-9",
            "yettoanswer": 1,
            "answered": 2
        }
    ],
}

注意,阶段和审阅集是对象数组。它可以是null,一个对象或多个对象。

我想得到" yettoanswer"和"回答"在每个"阶段"并在根。我要找的结果如下:

{
    "id": "chapter",
    "chapterName": "Example Chapter",
    "yettoanswer" : 22,
    "answered" : 31,
    "stages": [
        {
            "id": "stage",
            "yettoanswer" : 11,
            "answered" : 9,
            "stages": [
                {
                    "id": "stage",
                    "yettoanswer" : 2,
                    "answered" : 4,
                    "stages": [],
                    "reviewSets": [
                        {
                            "id":"reviewset-1",
                            "yettoanswer": 2,
                            "answered": 4
                        }
                    ],
                }
            ],
            "reviewSets": [
                {
                    "id": "reviewset-2",
                    "yettoanswer": 3,
                    "answered": 5
                },
                {
                    "id": "rewviewset-3",
                    "yettoanswer": 6,
                    "answered": 0
                }
            ]
        },
        {
            "id": "stage",
            "yettoanswer":11,
            "answered":18,
            "stages": [
                {
                    "id": "stage",
                    "yettoanswer":4,
                    "answered" : 12,
                    "stages": [],
                    "reviewSets": [
                        {
                            "id": "reviewset-4",
                            "yettoanswer": 0,
                            "answered": 8
                        },
                        {
                            "id": "reviewset-5",
                            "yettoanswer": 4,
                            "answered": 4
                        }                        
                    ]
                }
            ],
            "reviewSets": [
                {
                    "id": "reviewset-6",
                    "yettoanswer": 1,
                    "answered": 2
                },
                {
                    "id": "reviewset-7",
                    "yettoanswer": 6,
                    "answered": 4
                }
            ]
        }
    ],
    "reviewSets": [
        {
            "id": "reviewset-8",
            "yettoanswer": 0,
            "answered": 2
        },
        {
            "id": "reviewset-9",
            "yettoanswer": 1,
            "answered": 2
        }
    ],
}

我想使用javascript实现上述目标。

2 个答案:

答案 0 :(得分:0)

正如Nina在评论中提到的那样,您可能忘记在根中计算yettoanswer的{​​{1}} s,因为应该(按照您的逻辑)23,而不是22。

reviewSet

答案 1 :(得分:0)

您可以迭代相关键并构建子级别的总和。将属性分配给实际级别。

var object = { id: "chapter", chapterName: "Example Chapter", stages: [{ id: "stage", stages: [{ id: "stage", stages: [], reviewSets: [{ id: "reviewset-1", yettoanswer: 2, answered: 4 }], }], reviewSets: [{ id: "reviewset-2", yettoanswer: 3, answered: 5 }, { id: "rewviewset-3", yettoanswer: 6, answered: 0 }] }, { id: "stage", stages: [{ id: "stage", stages: [], reviewSets: [{ id: "reviewset-4", yettoanswer: 0, answered: 8 }, { id: "reviewset-5", yettoanswer: 4, answered: 4 }] }], reviewSets: [{ id: "reviewset-6", yettoanswer: 1, answered: 2 }, { id: "reviewset-7", yettoanswer: 6, answered: 4 }] }], reviewSets: [{ id: "reviewset-8", yettoanswer: 0, answered: 2 }, { id: "reviewset-9", yettoanswer: 1, answered: 2 }] };

[object].reduce(function sum(r, o) {
    var v = ['stages', 'reviewSets'].reduce(function (r, k) {
            return (o[k] || []).reduce(sum, r);
        }, { yettoanswer: 0, answered: 0 });

    Object.keys(r).forEach(function (k) {
        o[k] = (o[k] || 0) + v[k];
        r[k] += o[k];
    });
    return r;
}, { yettoanswer: 0, answered: 0 })

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }