以索引方式计算数组值的数组

时间:2018-06-06 09:24:27

标签: javascript arrays

我从服务器收到一个类似

的对象
    var response = [
         {
          "id":"1",
          "series":{
                    "eoy":[10,20,30,40,50,60],
                    "eom":[20,30,40,50,60],
                    "now":[30,40,50,60,70,80]
                   }
         }
         {
          "id":"2",
          "series":{
                   "eoy":[30,40,20,50,10],
                   "eom":[20,10,50,70,20,50],
                   "now":[30,10,70,20]
                   }
         }
    ]

里面的系列,有很多键和值。出于问题目的,我写了3对。现在我想以这样一种方式添加这些数组,我将得到的结果作为一个应该像

的对象
var result = {
     "eoy":[40,60,50,90,60,60],
     "eom":[40,40,90,120,80,50],
     "now":[60,50,120,80,70,80]
}

在此,我添加添加所有的eoys并将其作为一个数组并与每个系列成员一起进行。有没有办法制作所有计算值的单个对象。

4 个答案:

答案 0 :(得分:1)

您可以使用reduce对数组和对象进行分组。使用Object.entries将对象转换为数组。并使用forEach循环遍历数组。

let arr = [{"id": "1","series": {"eoy": [10, 20, 30, 40, 50, 60],"eom": [20, 30, 40, 50, 60],"now": [30, 40, 50, 60, 70, 80]}},{"id": "2","series": {"eoy": [30, 40, 20, 50, 10],"eom": [20, 10, 50, 70, 20, 50],"now": [30, 10, 70, 20]}}];

let result = arr.reduce((c, {series}) => {
  Object.entries(series).forEach(([k, v]) => {
    c[k] = c[k] || [];
    v.forEach((o, i) => {
      c[k][i] = c[k][i] || 0;
      c[k][i] += o;
    });
  })
  return c;
}, {});

console.log(result);

如果for不可用,您可以使用in Object.entries循环访问您的对象。

let arr = [{"id": "1","series": {"eoy": [10, 20, 30, 40, 50, 60],"eom": [20, 30, 40, 50, 60],"now": [30, 40, 50, 60, 70, 80]}},{"id": "2","series": {"eoy": [30, 40, 20, 50, 10],"eom": [20, 10, 50, 70, 20, 50],"now": [30, 10, 70, 20]}}];

let result = arr.reduce((c, {series}) => {
  for (var k in series) {
    c[k] = c[k] || [];
    series[k].forEach((o, i) => {
      c[k][i] = c[k][i] || 0;
      c[k][i] += o;
    });
  }
  return c;
}, {});

console.log(result);

答案 1 :(得分:0)

您可以使用reduce,如下所示:

const input=[{"id":"1","series":{"eoy":[10,20,30,40,50,60],"eom":[20,30,40,50,60],"now":[30,40,50,60,70,80]}},{"id":"2","series":{"eoy":[30,40,20,50,10],"eom":[20,10,50,70,20,50],"now":[30,10,70,20]}}];
const output = input.reduce((a, { series }) => {
  Object.entries(series).forEach(([key, arr]) => {
    if (!a[key]) a[key] = [];
    const sumArr = a[key];
    arr.forEach((num, i) => sumArr[i] = (sumArr[i] || 0) + num);
  });
  return a;
}, {});
console.log(output);
{
 "eoy":[40,60,50,90,60,60],
 "eom":[40,40,90,120,80,50],
 "now":[60,50,120,80,70,80]
}

答案 2 :(得分:0)

使用Array.reduceArray.forEachObject.entries

var arr = [{"id":"1","series":{"eoy":[10,20,30,40,50,60],"eom":[20,30,40,50,60],"now":[30,40,50,60,70,80]}},{"id":"2","series":{"eoy":[30,40,20,50,10],"eom":[20,10,50,70,20,50],"now":[30,10,70,20]}}];

let result = arr.reduce((a, c) => {
  /* For each item in the array, iterate over each entry
   ** in series and sum it in the accumulator object. */
  Object.keys(c.series).forEach((k) => {
    let v = c.series[k];
    a[k] = a[k] || []; 
    v.forEach((n, i) => a[k][i] = a[k][i] ? a[k][i] + n : n);
  });
  return a;
}, {});

console.log(result);

没有Object.entries

{{}}

答案 3 :(得分:-1)

您可以使用map执行此操作:

  

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。



var arr = [
 {
  "id":"1",
  "series": {
            "eoy":[10,20,30,40,50,60],
            "eom":[20,30,40,50,60],
            "now":[30,40,50,60,70,80]
           }
 },
 {
  "id":"2",
  "series":{
           "eoy":[30,40,20,50,10],
           "eom":[20,10,50,70,20,50],
           "now":[30,10,70,20]
           }
 }
];

console.log(arr.map(x=> x.series));