将属性添加到已分组的对象并取消分组

时间:2019-03-05 09:05:45

标签: javascript underscore.js

我正在使用underscore.js对对象进行分组,现在我想添加一个属性以用作该组的标识符,然后将这些对象还原为原始结构。但不确定如何做到。

规则是查找当天有多个约会的人并为其添加一个属性。

我们在这里实现的目标:

https://jsfiddle.net/bjxgszmw/

具有以下代码行:

var resultt = _.chain(allAppointments)
    .groupBy('appointment_date')
    .mapObject( date => _.groupBy(date, 'email' ) )

所以从我们那里得到的是:

{
  "23July": {
    "john@domain.com": [
      {
        "ap_id": 23,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"
      }
    ],
    "rose@domain.com": [
      {
        "ap_id": 55,

像这样简单的东西

allAppointments_Filtered: 
      [{
            "ap_id": 23,
            "name": "John",
            "email": "John@domain.com",
            "appointment_date": "23July",
            "appointment_category": 3,
            "time": "morning",
            hasMultipleAppointmentOnDate: "yes"

            },{

           "ap_id": 55,
           "name": "Rose",
           "email": "rose@domain.com",
           "appointment_date": "23July",
           "appointment_category": 4,
           "time": "afternoon"
            hasMultipleAppointmentOnDate: "nope"

            },{

           "ap_id": 44,
           "name": "John",
           "email": "john@domain.com",
           "appointment_date": "23July",
           "appointment_category": 4,
           "time": "afternoon"
            hasMultipleAppointmentOnDate: "yes"

            },{
              ...

      }];

2 个答案:

答案 0 :(得分:1)

那么,您不需要进行所有这些分组和映射。您要做的就是一张地图,并根据您当前检查的约会进行计数:

var allAppointments = [
 {
        "ap_id": 23,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"    
   },
      {
        "ap_id": 55,
        "name": "Rose",
        "email": "rose@domain.com",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 70,
        "name": "Kate",
        "email": "kate@domain.com",
        "appointment_date": "29July",
        "appointment_category": 4,
        "time": "afternon"        
      }
]

var counts = {};
var result = _.mapObject(allAppointments, (appointment) => {
    var key = appointment.email + appointment.appointment_date;
    
    if (!_.has(counts, key)) {
        counts[key] = _.countBy(allAppointments, (app) => 
            appointment.email === app.email && 
            appointment.appointment_date === app.appointment_date
        ).true > 1
    }
    
    appointment.hasMultipleAppointmentOnDate = counts[key];

    return appointment;
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

答案 1 :(得分:0)

您需要按组合键分组:

// data
const allAppointments = [
 {
        "ap_id": 23,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"    
   },
      {
        "ap_id": 55,
        "name": "Rose",
        "email": "rose@domain.com",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "john@domain.com",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 70,
        "name": "Kate",
        "email": "kate@domain.com",
        "appointment_date": "29July",
        "appointment_category": 4,
        "time": "afternon"        
      }
];

// gets grouping key, which is email + date
const groupKey = i => i.email +'_'+ i.appointment_date;

// store counts for appointments for unique (email + date)
const counts = _.countBy(allAppointments,groupKey);

// checks if appointment has more than one instances on date
const isMulti = i => counts[groupKey(i)] > 1;

// updated appointment with multiple indicator property
const multiProp = i => ({hasMultipleAppointmentOnDate: isMulti(i) ? "yes": "nope"});

// update initial array items with multiple 
const updated = _.map(allAppointments,i => _.extend(i,multiProp(i)));

// see results
console.log(updated);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>