Summarize array values, underscore

自闭症网瘾萝莉.ら 提交于 2019-12-25 18:38:08

问题


Got this array:

var arr =  [
      {
        series: [
          {
            "name": 2014,
            "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18]
          },
          {
            "name": 2015,
            "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30]
          },
        ]
      },
      {
        series: [
          {
            "name": 2014,
            "data": [32, 17, 15, 12, 33 10, 33, 10, 11, 14, 14, 18]
          },
          {
            "name": 2015,
            "data": [45, 10, 12, 55, 77, 7, 6, 8, 8, 11, 33, 30]
          },
        ]
      },
    ]

I need to create a function that returns a summrized series:

var series = [
  {
    year: '2014',
    data: [51, 34....],
  },
  {
    year: '2015',
    data: [63, 27....],
  }
]

I could to loops to do this but I guess there is some smart way of doing with underscore? Probably with the reduce function. Any ideas of how to do this?


回答1:


var arr = [{    
    "series": [{    
        "name": 2014,    
        "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18]    
    }, {    
        "name": 2015,    
        "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30]    
    }]    
}, {    
    "series": [{    
        "name": 2014,    
        "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18]    
    }, {    
        "name": 2015,    
        "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30]    
    }]    
}];    


_.chain(arr)    
    .map('series')    
    .flatten()    
    .reduce(function(object, value) {    
        object[value.name] = !object[value.name] ? value.data : _.union(object[value.data], value.data);    
        return object;    
    }, {})    
    .reduce(function(arr, value, key) {    
        arr.push({    
            year: key,    
            data: value    
        });    
        return arr;    
    }, [])    
    .value();    



回答2:


You could use some iterations and an object as reference to the year for grouping.

var arr = [{ series: [{ "name": 2014, "data": [19, 17, 15, 12, 10, 10, 12, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [18, 17, 16, 12, 10, 7, 6, 8, 8, 11, 15, 30] }] }, { series: [{ "name": 2014, "data": [32, 17, 15, 12, 33, 10, 33, 10, 11, 14, 14, 18] }, { "name": 2015, "data": [45, 10, 12, 55, 77, 7, 6, 8, 8, 11, 33, 30] }] }],
    series = [];
 
arr.forEach(function (a) {
    Object.keys(a).forEach(function (k) {
        a[k].forEach(function (b) {
            if (!this[b.name]) {
                this[b.name] = { year: b.name, data: [] };
                series.push(this[b.name]);
            }
            b.data.forEach(function (c, i) {
                this[b.name].data[i] = (this[b.name].data[i] || 0) + c;
            }, this);
        }, this);
    }, this);
}, {});
 
document.write('<pre>' + JSON.stringify(series, 0, 4) + '</pre>');
 


来源:https://stackoverflow.com/questions/36532483/summarize-array-values-underscore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!