数组连续元素总和

时间:2015-09-03 08:05:52

标签: javascript

我有一个如下所示的数组

var data = [{
    category: "2014-1-1",
    subCategory: [{
        name: "shoe",
        val: 70,
        toolTip: "toolTip-shoe-70",
        drill: false
    }, {
        name: "pant",
        val: 60,
        toolTip: "toolTip-pant-60",
        drill: false
    }, {
        name: "shirt",
        val: 50,
        toolTip: "toolTip-shirt-50",
        drill: false
    }, {
        name: "Tshirt",
        val: 40,
        toolTip: "toolTip-Tshirt-40",
        drill: true
    }, {
        name: "socks",
        val: 30,
        toolTip: "toolTip-socks-30",
        drill: false
    }, {
        name: "watchs",
        val: 20,
        toolTip: "toolTip-watchs-20",
        drill: false
    }, {
        name: "hats",
        val: 10,
        toolTip: "toolTip-hats-10",
        drill: false
    }]
}, {
    category: "2014-3-2",
    subCategory: [{
        name: "shoe",
        val: 16,
        toolTip: "toolTip-shoe-16",
        drill: false
    }, {
        name: "pant",
        val: 12,
        toolTip: "toolTip-pant-12",
        drill: false
    }, {
        name: "shirt",
        val: 14,
        toolTip: "toolTip-shirt-14",
        drill: false
    }, {
        name: "Tshirt",
        val: 13,
        toolTip: "toolTip-Tshirt-13",
        drill: false
    }, {
        name: "socks",
        val: 22,
        toolTip: "toolTip-socks-22",
        drill: true
    }, {
        name: "watchs",
        val: 31,
        toolTip: "toolTip-watchs-31",
        drill: false
    }, {
        name: "hats",
        val: 21,
        toolTip: "toolTip-hats-21",
        drill: false
    }]
}, {
    category: "2014-6-3",
    subCategory: [{
        name: "shoe",
        val: 22,
        toolTip: "toolTip-shoe-22",
        drill: false
    }, {
        name: "pant",
        val: 25,
        toolTip: "toolTip-pant-25",
        drill: false
    }, {
        name: "shirt",
        val: 28,
        toolTip: "toolTip-shirt-28",
        drill: false
    }, {
        name: "Tshirt",
        val: 33,
        toolTip: "toolTip-Tshirt-33",
        drill: false
    }, {
        name: "socks",
        val: 25,
        toolTip: "toolTip-socks-25",
        drill: false
    }, {
        name: "watchs",
        val: 40,
        toolTip: "toolTip-watchs-40",
        drill: false
    }, {
        name: "hats",
        val: 38,
        toolTip: "toolTip-hats-38",
        drill: true
    }]
}, {
    category: "2014-9-3",
    subCategory: [{
        name: "shoe",
        val: 25,
        toolTip: "toolTip-shoe-25",
        drill: false
    }, {
        name: "pant",
        val: 33,
        toolTip: "toolTip-pant-33",
        drill: false
    }, {
        name: "shirt",
        val: 37,
        toolTip: "toolTip-shirt-37",
        drill: false
    }, {
        name: "Tshirt",
        val: 38,
        toolTip: "toolTip-Tshirt-38",
        drill: false
    }, {
        name: "socks",
        val: 32,
        toolTip: "toolTip-socks-32",
        drill: false
    }, {
        name: "watchs",
        val: 28,
        toolTip: "toolTip-watchs-28",
        drill: true
    }, {
        name: "hats",
        val: 41,
        toolTip: "toolTip-hats-41",
        drill: false
    }]
}, {
    category: "2014-12-4",
    subCategory: [{
        name: "shoe",
        val: 22,
        toolTip: "toolTip-shoe-22",
        drill: false
    }, {
        name: "pant",
        val: 41,
        toolTip: "toolTip-pant-41",
        drill: false
    }, {
        name: "shirt",
        val: 33,
        toolTip: "toolTip-shirt-33",
        drill: false
    }, {
        name: "Tshirt",
        val: 22,
        toolTip: "toolTip-Tshirt-22",
        drill: false
    }, {
        name: "socks",
        val: 11,
        toolTip: "toolTip-socks-11",
        drill: false
    }, {
        name: "watchs",
        val: 39,
        toolTip: "toolTip-watchs-39",
        drill: false
    }, {
        name: "hats",
        val: 32,
        toolTip: "toolTip-hats-32",
        drill: false
    }]
}, {
    category: "2015-3-5",
    subCategory: [{
        name: "shoe",
        val: 33,
        toolTip: "toolTip-shoe-33",
        drill: false
    }, {
        name: "pant",
        val: 51,
        toolTip: "toolTip-pant-51",
        drill: false
    }, {
        name: "shirt",
        val: 43,
        toolTip: "toolTip-shirt-43",
        drill: false
    }, {
        name: "Tshirt",
        val: 43,
        toolTip: "toolTip-Tshirt-43",
        drill: false
    }, {
        name: "socks",
        val: 46,
        toolTip: "toolTip-socks-46",
        drill: false
    }, {
        name: "watchs",
        val: 47,
        toolTip: "toolTip-watchs-47",
        drill: false
    }, {
        name: "hats",
        val: 49,
        toolTip: "toolTip-hats-49",
        drill: false
    }]
}];  

我想在以下数组中转换此数组。

var newData = [{
    "category": "2014-1-1",
        "subCategory": [{
        "name": "shoe",
            "val": 70,
            "toolTip": "toolTip-shoe-70",
            "drill": false,
            "parent": "2014-1-1",
            "count": 70
    }, {
        "name": "pant",
            "val": 60,
            "toolTip": "toolTip-pant-60",
            "drill": false,
            "parent": "2014-1-1",
            "count": 60
    }, {
        "name": "shirt",
            "val": 50,
            "toolTip": "toolTip-shirt-50",
            "drill": false,
            "parent": "2014-1-1",
            "count": 50
    }, {
        "name": "Tshirt",
            "val": 40,
            "toolTip": "toolTip-Tshirt-40",
            "drill": true,
            "parent": "2014-1-1",
            "count": 40
    }, {
        "name": "socks",
            "val": 30,
            "toolTip": "toolTip-socks-30",
            "drill": false,
            "parent": "2014-1-1",
            "count": 30
    }, {
        "name": "watchs",
            "val": 20,
            "toolTip": "toolTip-watchs-20",
            "drill": false,
            "parent": "2014-1-1",
            "count": 20
    }, {
        "name": "hats",
            "val": 10,
            "toolTip": "toolTip-hats-10",
            "drill": false,
            "parent": "2014-1-1",
            "count": 10
    }]
}, {
    "category": "2014-3-2",
        "subCategory": [{
        "name": "shoe",
            "val": 16,
            "toolTip": "toolTip-shoe-16",
            "drill": false,
            "parent": "2014-3-2",
            "count": 86
    }, {
        "name": "pant",
            "val": 12,
            "toolTip": "toolTip-pant-12",
            "drill": false,
            "parent": "2014-3-2",
            "count": 72
    }, {
        "name": "shirt",
            "val": 14,
            "toolTip": "toolTip-shirt-14",
            "drill": false,
            "parent": "2014-3-2",
            "count": 64
    }, {
        "name": "Tshirt",
            "val": 13,
            "toolTip": "toolTip-Tshirt-13",
            "drill": false,
            "parent": "2014-3-2",
            "count": 53
    }, {
        "name": "socks",
            "val": 22,
            "toolTip": "toolTip-socks-22",
            "drill": true,
            "parent": "2014-3-2",
            "count": 52
    }, {
        "name": "watchs",
            "val": 31,
            "toolTip": "toolTip-watchs-31",
            "drill": false,
            "parent": "2014-3-2",
            "count": 51
    }, {
        "name": "hats",
            "val": 21,
            "toolTip": "toolTip-hats-21",
            "drill": false,
            "parent": "2014-3-2",
            "count": 31
    }]
}, {
    "category": "2014-6-3",
        "subCategory": [{
        "name": "shoe",
            "val": 22,
            "toolTip": "toolTip-shoe-22",
            "drill": false,
            "parent": "2014-6-3",
            "count": 108
    }, {
        "name": "pant",
            "val": 25,
            "toolTip": "toolTip-pant-25",
            "drill": false,
            "parent": "2014-6-3",
            "count": 97
    }, {
        "name": "shirt",
            "val": 28,
            "toolTip": "toolTip-shirt-28",
            "drill": false,
            "parent": "2014-6-3",
            "count": 92
    }, {
        "name": "Tshirt",
            "val": 33,
            "toolTip": "toolTip-Tshirt-33",
            "drill": false,
            "parent": "2014-6-3",
            "count": 86
    }, {
        "name": "socks",
            "val": 25,
            "toolTip": "toolTip-socks-25",
            "drill": false,
            "parent": "2014-6-3",
            "count": 77
    }, {
        "name": "watchs",
            "val": 40,
            "toolTip": "toolTip-watchs-40",
            "drill": false,
            "parent": "2014-6-3",
            "count": 91
    }, {
        "name": "hats",
            "val": 38,
            "toolTip": "toolTip-hats-38",
            "drill": true,
            "parent": "2014-6-3",
            "count": 69
    }]
}, {
    "category": "2014-9-3",
        "subCategory": [{
        "name": "shoe",
            "val": 25,
            "toolTip": "toolTip-shoe-25",
            "drill": false,
            "parent": "2014-9-3",
            "count": 133
    }, {
        "name": "pant",
            "val": 33,
            "toolTip": "toolTip-pant-33",
            "drill": false,
            "parent": "2014-9-3",
            "count": 130
    }, {
        "name": "shirt",
            "val": 37,
            "toolTip": "toolTip-shirt-37",
            "drill": false,
            "parent": "2014-9-3",
            "count": 129
    }, {
        "name": "Tshirt",
            "val": 38,
            "toolTip": "toolTip-Tshirt-38",
            "drill": false,
            "parent": "2014-9-3",
            "count": 124
    }, {
        "name": "socks",
            "val": 32,
            "toolTip": "toolTip-socks-32",
            "drill": false,
            "parent": "2014-9-3",
            "count": 109
    }, {
        "name": "watchs",
            "val": 28,
            "toolTip": "toolTip-watchs-28",
            "drill": true,
            "parent": "2014-9-3",
            "count": 119
    }, {
        "name": "hats",
            "val": 41,
            "toolTip": "toolTip-hats-41",
            "drill": false,
            "parent": "2014-9-3",
            "count": 110
    }]
}, {
    "category": "2014-12-4",
        "subCategory": [{
        "name": "shoe",
            "val": 22,
            "toolTip": "toolTip-shoe-22",
            "drill": false,
            "parent": "2014-12-4",
            "count": 155
    }, {
        "name": "pant",
            "val": 41,
            "toolTip": "toolTip-pant-41",
            "drill": false,
            "parent": "2014-12-4",
            "count": 171
    }, {
        "name": "shirt",
            "val": 33,
            "toolTip": "toolTip-shirt-33",
            "drill": false,
            "parent": "2014-12-4",
            "count": 162
    }, {
        "name": "Tshirt",
            "val": 22,
            "toolTip": "toolTip-Tshirt-22",
            "drill": false,
            "parent": "2014-12-4",
            "count": 146
    }, {
        "name": "socks",
            "val": 11,
            "toolTip": "toolTip-socks-11",
            "drill": false,
            "parent": "2014-12-4",
            "count": 120
    }, {
        "name": "watchs",
            "val": 39,
            "toolTip": "toolTip-watchs-39",
            "drill": false,
            "parent": "2014-12-4",
            "count": 158
    }, {
        "name": "hats",
            "val": 32,
            "toolTip": "toolTip-hats-32",
            "drill": false,
            "parent": "2014-12-4",
            "count": 142
    }]
}, {
    "category": "2015-3-5",
        "subCategory": [{
        "name": "shoe",
            "val": 33,
            "toolTip": "toolTip-shoe-33",
            "drill": false,
            "parent": "2015-3-5",
            "count": 188
    }, {
        "name": "pant",
            "val": 51,
            "toolTip": "toolTip-pant-51",
            "drill": false,
            "parent": "2015-3-5",
            "count": 222
    }, {
        "name": "shirt",
            "val": 43,
            "toolTip": "toolTip-shirt-43",
            "drill": false,
            "parent": "2015-3-5",
            "count": 205
    }, {
        "name": "Tshirt",
            "val": 43,
            "toolTip": "toolTip-Tshirt-43",
            "drill": false,
            "parent": "2015-3-5",
            "count": 189
    }, {
        "name": "socks",
            "val": 46,
            "toolTip": "toolTip-socks-46",
            "drill": false,
            "parent": "2015-3-5",
            "count": 166
    }, {
        "name": "watchs",
            "val": 47,
            "toolTip": "toolTip-watchs-47",
            "drill": false,
            "parent": "2015-3-5",
            "count": 228
    }, {
        "name": "hats",
            "val": 49,
            "toolTip": "toolTip-hats-49",
            "drill": false,
            "parent": "2015-3-5",
            "count": 191
    }]
}];  

在新数组subCategory中还有两个属性parentcountParent值为Category值,Count为之前的总和subCategory val
到目前为止,我可以在parent中添加subCategory属性。我无法添加count属性 这是JS代码

var data = [{
    category: "2014-1-1",
    subCategory: [{
        name: "shoe",
        val: 70,
        toolTip: "toolTip-shoe-70",
        drill: false
    }, {
        name: "pant",
        val: 60,
        toolTip: "toolTip-pant-60",
        drill: false
    }, {
        name: "shirt",
        val: 50,
        toolTip: "toolTip-shirt-50",
        drill: false
    }, {
        name: "Tshirt",
        val: 40,
        toolTip: "toolTip-Tshirt-40",
        drill: true
    }, {
        name: "socks",
        val: 30,
        toolTip: "toolTip-socks-30",
        drill: false
    }, {
        name: "watchs",
        val: 20,
        toolTip: "toolTip-watchs-20",
        drill: false
    }, {
        name: "hats",
        val: 10,
        toolTip: "toolTip-hats-10",
        drill: false
    }]
}, {
    category: "2014-3-2",
    subCategory: [{
        name: "shoe",
        val: 16,
        toolTip: "toolTip-shoe-16",
        drill: false
    }, {
        name: "pant",
        val: 12,
        toolTip: "toolTip-pant-12",
        drill: false
    }, {
        name: "shirt",
        val: 14,
        toolTip: "toolTip-shirt-14",
        drill: false
    }, {
        name: "Tshirt",
        val: 13,
        toolTip: "toolTip-Tshirt-13",
        drill: false
    }, {
        name: "socks",
        val: 22,
        toolTip: "toolTip-socks-22",
        drill: true
    }, {
        name: "watchs",
        val: 31,
        toolTip: "toolTip-watchs-31",
        drill: false
    }, {
        name: "hats",
        val: 21,
        toolTip: "toolTip-hats-21",
        drill: false
    }]
}, {
    category: "2014-6-3",
    subCategory: [{
        name: "shoe",
        val: 22,
        toolTip: "toolTip-shoe-22",
        drill: false
    }, {
        name: "pant",
        val: 25,
        toolTip: "toolTip-pant-25",
        drill: false
    }, {
        name: "shirt",
        val: 28,
        toolTip: "toolTip-shirt-28",
        drill: false
    }, {
        name: "Tshirt",
        val: 33,
        toolTip: "toolTip-Tshirt-33",
        drill: false
    }, {
        name: "socks",
        val: 25,
        toolTip: "toolTip-socks-25",
        drill: false
    }, {
        name: "watchs",
        val: 40,
        toolTip: "toolTip-watchs-40",
        drill: false
    }, {
        name: "hats",
        val: 38,
        toolTip: "toolTip-hats-38",
        drill: true
    }]
}, {
    category: "2014-9-3",
    subCategory: [{
        name: "shoe",
        val: 25,
        toolTip: "toolTip-shoe-25",
        drill: false
    }, {
        name: "pant",
        val: 33,
        toolTip: "toolTip-pant-33",
        drill: false
    }, {
        name: "shirt",
        val: 37,
        toolTip: "toolTip-shirt-37",
        drill: false
    }, {
        name: "Tshirt",
        val: 38,
        toolTip: "toolTip-Tshirt-38",
        drill: false
    }, {
        name: "socks",
        val: 32,
        toolTip: "toolTip-socks-32",
        drill: false
    }, {
        name: "watchs",
        val: 28,
        toolTip: "toolTip-watchs-28",
        drill: true
    }, {
        name: "hats",
        val: 41,
        toolTip: "toolTip-hats-41",
        drill: false
    }]
}, {
    category: "2014-12-4",
    subCategory: [{
        name: "shoe",
        val: 22,
        toolTip: "toolTip-shoe-22",
        drill: false
    }, {
        name: "pant",
        val: 41,
        toolTip: "toolTip-pant-41",
        drill: false
    }, {
        name: "shirt",
        val: 33,
        toolTip: "toolTip-shirt-33",
        drill: false
    }, {
        name: "Tshirt",
        val: 22,
        toolTip: "toolTip-Tshirt-22",
        drill: false
    }, {
        name: "socks",
        val: 11,
        toolTip: "toolTip-socks-11",
        drill: false
    }, {
        name: "watchs",
        val: 39,
        toolTip: "toolTip-watchs-39",
        drill: false
    }, {
        name: "hats",
        val: 32,
        toolTip: "toolTip-hats-32",
        drill: false
    }]
}, {
    category: "2015-3-5",
    subCategory: [{
        name: "shoe",
        val: 33,
        toolTip: "toolTip-shoe-33",
        drill: false
    }, {
        name: "pant",
        val: 51,
        toolTip: "toolTip-pant-51",
        drill: false
    }, {
        name: "shirt",
        val: 43,
        toolTip: "toolTip-shirt-43",
        drill: false
    }, {
        name: "Tshirt",
        val: 43,
        toolTip: "toolTip-Tshirt-43",
        drill: false
    }, {
        name: "socks",
        val: 46,
        toolTip: "toolTip-socks-46",
        drill: false
    }, {
        name: "watchs",
        val: 47,
        toolTip: "toolTip-watchs-47",
        drill: false
    }, {
        name: "hats",
        val: 49,
        toolTip: "toolTip-hats-49",
        drill: false
    }]
}];

function constructDataWithSum(userData) {
    debugger
    var arr = [];
    var distinctSubCategories = [];
    for (var i = 0; i < userData.length; i++) {
        for (var j = 0; j < userData[i].subCategory.length; j++) {
            //userData[i].subCategory[j].count = previousValue;
            userData[i].subCategory[j].parent = userData[i].category;
            if (customIndexOf(distinctSubCategories, userData[i].subCategory[j].name) == -1) {
                distinctSubCategories.push(userData[i].subCategory[j].name);
            }
        }
    }

}

function customIndexOf(dataArray, key) {
    var index = -1;
    for (var i = 0; i < dataArray.length; i++) {
        if (dataArray[i] == key) {
            index = i;
            break;
        }
    }

    return index;
}
constructDataWithSum(data);
console.log(JSON.stringify(data));  

Fiddle Link

2 个答案:

答案 0 :(得分:1)

您可以使用键/值对象(如

)保留每个子类别的val之和
function constructDataWithSum(userData) {
    var sum = {};
    for (var i = 0; i < userData.length; i++) {
        for (var j = 0; j < userData[i].subCategory.length; j++) {
            //userData[i].subCategory[j].count = previousValue;
            userData[i].subCategory[j].parent = userData[i].category;
            sum[userData[i].subCategory[j].name] = (sum[userData[i].subCategory[j].name]||0) + userData[i].subCategory[j].val;
            userData[i].subCategory[j].count = sum[userData[i].subCategory[j].name];
        }
    }

}

演示:Fiddle

答案 1 :(得分:0)

由于您在子类别上循环,您可以通过重置每个类别的计数器previousValue来保持计数,如下所示:

for (var i = 0; i < userData.length; i++) {
    var previousValue = 0;
    for (var j = 0; j < userData[i].subCategory.length; j++) {
        previousValue += userData[i].subCategory[j].val;
        userData[i].subCategory[j].count = previousValue;
        ...
    }
}

Updated fiddle here