按通用属性对对象数组进行分组

时间:2020-08-03 08:30:05

标签: javascript json

我只想获取现有周的计数,我想针对特定周的月份进行计算。 现在我可以获取每个月的每周计数。现在我只想显示现有周的星期。现在显示0。例如;可能具有week1和week 2的值。我只想显示出week1和week 2的计数可能需要一个月。7月需要显示第1周和第3周

const response=[
  {
    "UserName": "User1",
    "week": "Week 1",
    "Type": "type3",
    "months": "May",
    "count": 2
  },
  {
    "UserName": "User1",
    "week": "Week 2",
    "Type": "type1",
    "months": "Jun",
    "count": 1
  },
  {
    "UserName": "User1",
    "week": "Week 1",
    "Type": "type2",
    "months": "Jun",
    "count": 1
  },
  {
    "UserName": "User1",
    "week": "Week 2",
    "Type": "type1",
    "months": "May",
    "count": 1
  },
  {
    "UserName": "User1",
    "week": "Week 3",
    "Type": "type2",
    "months": "July",
    "count": 1
  },
  {
    "UserName": "User2",
    "week": "Week 1",
    "Type": "type3",
    "months": "May",
    "count": 2
  },
  {
    "UserName": "User2",
    "week": "Week 2",
    "Type": "type1",
    "months": "Jun",
    "count": 1
  },
  {
    "UserName": "User2",
    "week": "Week 1",
    "Type": "type2",
    "months": "Jun",
    "count": 1
  },
  {
    "UserName": "User2",
    "week": "Week 2",
    "Type": "type1",
    "months": "May",
    "count": 1
  },
  {
    "UserName": "User2",
    "week": "Week 1",
    "Type": "type2",
    "months": "July",
    "count": 1
  }
];
const WEEKS = ["Week 1", "Week 2", "Week 3", "Week 4"];
const result = response.reduce((acc, obj) => {
    existingObj = acc.find(ele => ele.UserName == obj.UserName && ele.month == obj.months && ele.Type == obj.Type)
    if (existingObj) {
        existingObj.Week1 += WEEKS.indexOf(obj.week) == 0 ? obj.count : 0;
        existingObj.Week2 += WEEKS.indexOf(obj.week) == 1 ? obj.count : 0;
        existingObj.Week3 += WEEKS.indexOf(obj.week) == 2 ? obj.count : 0;
        existingObj.Week4 += WEEKS.indexOf(obj.week) == 3 ? obj.count : 0;
    } else {
        acc.push({
            UserName: obj.UserName,
            month: obj.months,
            Type: obj.Type,
            Week1: WEEKS.indexOf(obj.week) == 0 ? obj.count : 0,
            Week2: WEEKS.indexOf(obj.week) == 1 ? obj.count : 0,
            Week3: WEEKS.indexOf(obj.week) == 2 ? obj.count : 0,
            Week4: WEEKS.indexOf(obj.week) == 3 ? obj.count : 0,
        });
    }
    return acc;
}, []);

console.log(result);

预期为

[{"UserName":"user1","month":"May","Type":"Type3","Week1":2,"Week3":3},
          {"UserName":"user2","month":"May","Type":"Type3","Week1":2},
          {"UserName":"user1","month":"May","Type":"Type1","Week2":1},
          {"UserName":"user2","month":"May","Type":"Type1","Week2":1,},
          {"UserName":"user1","month":"Jun","Type":"Type1","Week2":1},
          {"UserName":"user2","month":"Jun","Type":"Type1","Week2":1},
          {"UserName":"user1","month":"Jun","Type":"Type2","Week1":1},
          {"UserName":"user2","month":"Jun","Type":"Type2","Week1":1},
          {"UserName":"user1","month":"Jul","Type":"Type2","Week3":1},
          {"UserName":"user1","month":"Jul","Type":"Type2","Week1":1},
         ]

2 个答案:

答案 0 :(得分:1)

如果要通过UserNamemonth属性对源数组中的记录进行重组,则可以使用复合键(包含Map)来构建Array.prototype.reduce()(使用Map.prototype.values()UserNamemonth)和相应分组对象的值,然后用see docs将这些对象提取到数组中:

const src = [{"UserName":"User1","week":"Week 1","Type":"type3","months":"May","count":2},{"UserName":"User1","week":"Week 2","Type":"type1","months":"Jun","count":1},{"UserName":"User1","week":"Week 1","Type":"type2","months":"Jun","count":1},{"UserName":"User1","week":"Week 2","Type":"type1","months":"May","count":1},{"UserName":"User1","week":"Week 3","Type":"type2","months":"July","count":1},{"UserName":"User2","week":"Week 1","Type":"type3","months":"May","count":2},{"UserName":"User2","week":"Week 2","Type":"type1","months":"Jun","count":1},{"UserName":"User2","week":"Week 1","Type":"type2","months":"Jun","count":1},{"UserName":"User2","week":"Week 2","Type":"type1","months":"May","count":1},{"UserName":"User2","week":"Week 1","Type":"type2","months":"July","count":1}],

      result = [...src
        .reduce((r, {UserName, week: w, Type, months, count}) => {
          const key = UserName+"\ud8ff"+months,
                week = w.replace(' ', ''),
                grouppedRecord = r.get(key)
          grouppedRecord ?
          Object.assign(grouppedRecord, {[week]: count}) :
          r.set(key, {UserName, Type, month: months, [week]: count})
          return r
        }, new Map)
        .values()
      ]
      
console.log(result)
.as-console-wrapper{min-height:100%;}

答案 1 :(得分:0)

您可以先过滤掉想要的内容,然后使用过滤后的查找关键字遍历整个内容。

就像过滤五月的所有星期一样。然后将其用于进一步的工作。

var existingWeeks = response.filter(function(obj) {
    return (obj.months === "May") && (obj.User === "User1");
});