对象数组,组合键匹配的值。与下划线

时间:2017-01-05 23:30:45

标签: javascript ecmascript-6 underscore.js

给出以下数组:

[  
 {  
      "real":104.1530776708426,
      "workHour":8,
      "value":null
   },
   {  
      "real":71.53948769310401,
      "workHour":9
   },
   {  
      "real":97.84076993321577,
      "workHour":10
   },
   {  
      "real":115.72564185649178,
      "workHour":11
   },
   {  
      "real":79.95589800993977,
      "workHour":12
   },
   {  
      "real":91.52846219558896,
      "workHour":13
   },
   {  
      "real":57.86282092824589,
      "workHour":14
   },
   {  
      "real":148.33923183423036,
      "workHour":15
   },
   {  
      "real":125.19410346293202,
      "workHour":16
   },
   {  
      "real":67.33128253468612,
      "workHour":17
   },
   {  
      "real":55.75871834903695,
      "workHour":18
   },
   {  
      "real":102.04897509163365,
      "workHour":19
   },
   {  
      "real":132.55846249016332,
      "workHour":20
   },
   {  
      "real":138.87077022779013,
      "workHour":21
   },
   {  
      "real":60,
      "workHour":8
   },
   {  
      "real":52,
      "workHour":9
   },
   {  
      "real":114,
      "workHour":10
   },
   {  
      "real":115,
      "workHour":11
   },
   {  
      "real":92,
      "workHour":12
   },
   {  
      "real":102,
      "workHour":13
   },
   {  
      "real":54,
      "workHour":14
   },
   {  
      "real":62,
      "workHour":15
   },
   {  
      "real":133,
      "workHour":16
   },
   {  
      "real":116,
      "workHour":17
   },
   {  
      "real":106,
      "workHour":18
   },
   {  
      "real":115,
      "workHour":19
   },
   {  
      "real":115,
      "workHour":20
   },
   {  
      "real":125,
      "workHour":21
   }
]

如何找到workHour匹配的位置,并将real组合在一起? 我用纯JS做了它

const array = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];
var map = {};
    for (var i=0; i<array.length; i++) {
        var obj = array[i],
            id = obj.workHour;
        if (id in map) { // we know this id already
            // get the object and sum properties
            map[id].real += obj.real;
        } else // create a new one
            map[id] = {
                workHour: id,
                real: obj.real,
           };
    }
console.log(map)

如何使用ES6进行操作?还是下划线?

2 个答案:

答案 0 :(得分:1)

使用Array#reduce循环数组以创建具有组合值的新对象:

var data = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];


var result = data.reduce(function(r, o) {
  if (r[o.workHour]) {
    r[o.workHour].real += o.real
  } else {
    r[o.workHour] = {
      workHour: o.workHour,
      real: o.real
    }
  }

  return r;
}, {});

console.log(result);

答案 1 :(得分:1)

使用lodash(/ underscore),它使用_.reduce()_.clone()来构建聚合对象。然后使用_.values()将此对象的值输出为与输入数组类似的对象数组。

&#13;
&#13;
var data = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];

var result = _.values(_.reduce(data, (sumObj, curr) => {
  if (sumObj[curr.workHour]) 
    sumObj[curr.workHour].real += curr.real;
  else    
    sumObj[curr.workHour] = _.clone(curr);
  
  return sumObj;  
}, {}));

console.log(result);
&#13;
.as-console-wrapper{top:0;max-height:100%!important;}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;