title: Array consecutive elements sum
body: <p>I have an array like below  </p>
  
  <pre><code>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
      }]
  }];  
  </code></pre>
  
  <p>And I want to convert this array in the following array.  </p>
  
  <pre><code>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
      }]
  }];  
  </code></pre>
  
  <p>In new array <code>subCategory</code> have two more properties <code>parent</code> and <code>count</code>, <code>Parent</code> value is <code>Category</code> value and <code>Count</code> is sum of previous <code>subCategory val</code>.<br>
  So far I have able to add <code>parent</code> property in <code>subCategory</code>. I am not been able to add <code>count</code> property.<br>
  Here is JS code  </p>
  
  <pre><code>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 &lt; userData.length; i++) {
          for (var j = 0; j &lt; 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 &lt; dataArray.length; i++) {
          if (dataArray[i] == key) {
              index = i;
              break;
          }
      }
  
      return index;
  }
  constructDataWithSum(data);
  console.log(JSON.stringify(data));  
  </code></pre>
  
  <p><a href="http://jsfiddle.net/rsjb2y1n/3/" rel="nofollow"><code>Fiddle Link</code></a></p>