Javascript根据条件减少到对象组

时间:2016-12-20 07:00:32

标签: javascript

我有以下数据:

var foo = [
    {'MonthNo' : 03, 'CustomerTypeID' : 1 , 'TotalSales' : 45 },
    {'MonthNo' : 03, 'CustomerTypeID' : 2 , 'TotalSales' : 64 },
    {'MonthNo' : 03, 'CustomerTypeID' : 4 , 'TotalSales' : 89 },
    {'MonthNo' : 03, 'CustomerTypeID' : 5 , 'TotalSales' : 42 },
    {'MonthNo' : 04, 'CustomerTypeID' : 1 , 'TotalSales' : 66 },
    {'MonthNo' : 04, 'CustomerTypeID' : 2 , 'TotalSales' : 78 },
    {'MonthNo' : 04, 'CustomerTypeID' : 5 , 'TotalSales' : 20 },
    {'MonthNo' : 04, 'CustomerTypeID' : 6 , 'TotalSales' : 10 }
];

我想将其转换为以下内容

{'MonthNo' : 03, 'CustomerTypeID' : 1 , 'TotalSales' : 198 },
{'MonthNo' : 03, 'CustomerTypeID' : 5 , 'TotalSales' : 42 },
{'MonthNo' : 04, 'CustomerTypeID' : 1 , 'TotalSales' : 144 },
{'MonthNo' : 04, 'CustomerTypeID' : 5 , 'TotalSales' : 20 },
{'MonthNo' : 04, 'CustomerTypeID' : 6 , 'TotalSales' : 10 }

对于每个MonthNo,除了CustomerTypeID 5& 6,我想加上TotalSales值。 对于Ex:MonthNo 03,对于CustomerTypeID 1,2,4,它们的TotalSales值总计为198.对于MonthNo 04,CustomerTypeID 1的TotalSales值和& 2加起来为144。

我尝试使用reduce功能

var result = foo.reduce(function(obj,item){
    // code here
},{});

然而,我无法将它们隔离以获得所需的结果。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

您可以检查特殊情况并使用哈希表来引用正确的对象进行计数。



var foo = [{ MonthNo: '03', CustomerTypeID: 1, TotalSales: 45 }, { MonthNo: '03', CustomerTypeID: 2, TotalSales: 64 }, { MonthNo: '03', CustomerTypeID: 4, TotalSales: 89 }, { MonthNo: '03', CustomerTypeID: 5, TotalSales: 42 }, { MonthNo: '04', CustomerTypeID: 1, TotalSales: 66 }, { MonthNo: '04', CustomerTypeID: 2, TotalSales: 78 }, { MonthNo: '04', CustomerTypeID: 5, TotalSales: 20 }, { MonthNo: '04', CustomerTypeID: 6, TotalSales: 10 }],
    result = foo.reduce(function (hash) {
        return function (r, a) {
            var cType = [5, 6].indexOf(a.CustomerTypeID) === -1 ? 1 : a.CustomerTypeID,
                key = [a.MonthNo, cType].join('|');

            if (!hash[key]) {
                hash[key] = { MonthNo: a.MonthNo, CustomerTypeID: cType, TotalSales: 0 };
                r.push(hash[key]);
            }
            hash[key].TotalSales += a.TotalSales;
            return r;
        }
    }(Object.create(null)), []);
 
console.log(result);

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




答案 1 :(得分:0)

answer={};
for(i=0;i<foo.length;i++){
elem=foo[i];
answer[elem["MonthNo"]]=answer[elem["MonthNo"]]||{};
answer[elem["MonthNo"]][elem["CustomerTypeId"]]=answer[elem["MonthNo"]][elem["CustomerTypeId"]]||0;
     answer[elem["MonthNo"]][elem["CustomerTypeId"]]+=elem["TotalSales"];
}

这将导致:

answer:{
   15 (monthNo):{
       12 (Customer type id):123(Total Sales)
     }}

现在可以打印(或推送到数组):

for(month in answer){
 for(id in answer[month]){
   console.log(month+" "+id+":"+answer[month][id];
 }}

答案 2 :(得分:0)

您可以检查现有数组monthno和customerTypeID,并相应地相加总计

检查以下代码段

var foo = [{
  'MonthNo': 03,
  'CustomerTypeID': 1,
  'TotalSales': 45
}, {
  'MonthNo': 03,
  'CustomerTypeID': 2,
  'TotalSales': 64
}, {
  'MonthNo': 03,
  'CustomerTypeID': 4,
  'TotalSales': 89
}, {
  'MonthNo': 03,
  'CustomerTypeID': 5,
  'TotalSales': 42
}, {
  'MonthNo': 04,
  'CustomerTypeID': 1,
  'TotalSales': 66
}, {
  'MonthNo': 04,
  'CustomerTypeID': 2,
  'TotalSales': 78
}, {
  'MonthNo': 04,
  'CustomerTypeID': 5,
  'TotalSales': 20
}, {
  'MonthNo': 04,
  'CustomerTypeID': 6,
  'TotalSales': 10
}];
var reducedfoo = foo.reduce(function(previous, current, index) {
  var index = checkExistence(previous, current)
  if (index > -1)
    previous[index].TotalSales += current.TotalSales;
  else
    previous.push(current);
  return previous;
}, []);

function checkExistence(prev, curr) {
  var index = -1;
  Object.keys(prev).forEach(function(item) {
    if (prev[item].MonthNo === curr.MonthNo && curr.CustomerTypeID != 5 && curr.CustomerTypeID != 6)
      index = item;
  });
  return index;
}

console.log(reducedfoo);

希望有所帮助

答案 3 :(得分:0)

这可能会帮助您按MonthNo

对foo数组进行排序

&#13;
&#13;
std::cout << "Git-SHA1: " << GITSHA1REF << std::endl;
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用reduce的两步功能方法:

var groups = foo.reduce(function(groups,item){
  var newGroup;
  var groupId = item.MonthNo;
  if (item.CustomerTypeID === 5 || item.CustomerTypeID === 6) {
    groupId = groupId + '-' + item.CustomerTypeID;
  }
  if (!groups[groupId]) {
    groups[groupId] = [];
  }
  groups[groupId].push(item);
  return groups;
},{});

var result = Object.keys(groups).reduce(function(res, groupId) {
  var merged = groups[groupId].reduce(function(merged, item) { 
    if (!merged.MonthNo) {
        merged.MonthNo = item.MonthNo;
    }
    if (!merged.CustomerTypeID) {
        merged.CustomerTypeID = item.CustomerTypeID;
    }
    merged.TotalSales = merged.TotalSales + item.TotalSales;
    return merged;
  }, {TotalSales: 0});
  res.push(merged);
  return res;
}, []);

console.log(result);

注意:使用ES6,这样看起来会更好。