JavaScript添加键值对的值

时间:2018-02-19 21:30:08

标签: javascript arrays

我有一个这样的数组:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

我想从Array行中添加所有值 示例:0:{20: 24, 30: 37, 40: 17, 50: 16, 60: 5, 70: 2, day: "1"}

24+37+17+16+5+2= 101 

剩余价值相同并存储它们 101,......所有附加值

3 个答案:

答案 0 :(得分:0)

如果这个JSON有效,我会尝试这样的

function addValues(object) {
  let total = 0
  for (const [key, value] of Object.entries(obj)) {
    total += parseInt(value)
  }
  return total
}

然后,插入您的对象

addValues(myArray[0])

...编辑: 我将循环遍历数组中的所有项,在每个对象上调用addTotals()并返回最终值。像这样:

function() {
  let total = 0
  myArray.forEach(obj => {
    total += addValues(obj)
  })
  return total
}

答案 1 :(得分:0)

在我公开时,使用JSON是有效的,并且您希望将结果放在另一个数组中:

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]);
}

答案 2 :(得分:0)

您可以使用reducemap功能。

使用每个对象的总和创建一个新数组。

var array = [    {        "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"    }];

var result = array.map((o) => Object.keys(o).reduce((a, k) => a + (k === "day" ? 0 : o[k]), 0) );

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