[Solved] JavaScript adding values of key value pair [closed]


Suposing the JSON was valid as I expose, and you want the result in another array:

var foo = [
{20: 24, 30: 37, 40: 17, 50: 16, 60: 5, 70: 2, day: "1"},

{20: 69, 30: 163, 40: 88, 50: 42, 60: 27, 80: 2, "-1": 3, day: "2"},

{20: 78, 30: 220, 40: 88, 50: 49, 60: 22, 70: 1, 130: 2, "-1": 9, day: "3"},

{20: 14, 30: 18, 40: 7, 50: 10, 60: 3, day: "4"},

{20: 21, 30: 63, 40: 22, 50: 9, 60: 5, "-1": 1, day: "5"},

{20: 12, 30: 38, 40: 10, 50: 13, 60: 1, "-1": 1, day: "6"},

{20: 23, 30: 39, 40: 21, 50: 5, 60: 1, "-1": 1, day: "7"},

{20: 57, 30: 156, 40: 67, 50: 19, 60: 17, 70: 1, 80: 1, "-1": 5, day: "8"},

{20: 67, 30: 169, 40: 86, 50: 56, 60: 30, "-1": 6, day: "9"},

{20: 101, 30: 209, 40: 99, 50: 56, 60: 34, "-1": 10, day: "10"},

{10: 1, 20: 108, 30: 223, 40: 134, 50: 60, 60: 40, 70: 3, 80: 2, 130: 3, "-1": 10, day: "11"},

{10: 2, 20: 73, 30: 158, 40: 72, 50: 39, 60: 22, "-1": 11, day: "12"},

{20: 66, 30: 126, 40: 59, 50: 26, 60: 12, 70: 2, "-1": 11, day: "13"},

{10: 1, 20: 36, 30: 85, 40: 34, 50: 10, 60: 11, "-1": 14, day: "14"},

{10: 1, 20: 57, 30: 146, 40: 43, 50: 33, 60: 12, 70: 1, "-1": 4, day: "15"},

{20: 91, 30: 209, 40: 132, 50: 56, 60: 30, 70: 1, 80: 1, "-1": 5, day: "16"},

{20: 74, 30: 185, 40: 73, 50: 34, 60: 21, 70: 1, 80: 1, "-1": 13, day: "17"},

{20: 78, 30: 208, 40: 101, 50: 41, 60: 34, 80: 1, "-1": 7, day: "18"},

{20: 85, 30: 244, 40: 111, 50: 53, 60: 35, 70: 1, "-1": 13, day: "19"},

{20: 87, 30: 191, 40: 113, 50: 49, 60: 26, 70: 2, 130: 1, "-1": 37, day: "20"},

{20: 91, 30: 170, 40: 77, 50: 45, 60: 18, 70: 2, "-1": 54, day: "21"},

{20: 126, 30: 284, 40: 134, 50: 68, 60: 41, 80: 2, "-1": 11, day: "22"},

{20: 101, 30: 238, 40: 100, 50: 50, 60: 29, 80: 1, "-1": 5, day: "23"},

{20: 131, 30: 265, 40: 125, 50: 54, 60: 27, 80: 1, "-1": 12, day: "24"},

{20: 91, 30: 244, 40: 138, 50: 66, 60: 29, 70: 1, 80: 1, 130: 2, "-1": 13, day: "25"},

{20: 95, 30: 269, 40: 123, 50: 53, 60: 33, 130: 1, "-1": 14, day: "26"},

{20: 110, 30: 221, 40: 83, 50: 42, 60: 23, 80: 2, "-1": 26, day: "27"},

{20: 70, 30: 130, 40: 38, 50: 33, 60: 13, 80: 1, "-1": 2, day: "28"},

{20: 122, 30: 310, 40: 160, 50: 53, 60: 31, 80: 2, 130: 1, "-1": 15, day: "29"},

{20: 91, 30: 204, 40: 97, 50: 59, 60: 21, "-1": 13, day: "30"},

{20: 97, 30: 206, 40: 125, 50: 53, 60: 27, 130: 1, "-1": 16, day: "31"}
];

let bar = [];
for (let i = 0; i < foo.length; i++) {
  let sum = 0;
    for (let property in foo[i]) {
    if (foo[i].hasOwnProperty(property) && property != 'day') {
        // do stuff
        sum += foo[i][property];
    }
  }
  bar.push(sum);
  console.log(bar[i]);
}

0

solved JavaScript adding values of key value pair [closed]