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

问题描述 投票:0回答:2

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

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

我们在这里取得的成就:

https://jsfiddle.net/bjxgszmw/

用这行代码:

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

所以从我们得到的是:

{
  "23July": {
    "[email protected]": [
      {
        "ap_id": 23,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"
      }
    ],
    "[email protected]": [
      {
        "ap_id": 55,

这样简单的事情;

allAppointments_Filtered: 
      [{
            "ap_id": 23,
            "name": "John",
            "email": "[email protected]",
            "appointment_date": "23July",
            "appointment_category": 3,
            "time": "morning",
            hasMultipleAppointmentOnDate: "yes"

            },{

           "ap_id": 55,
           "name": "Rose",
           "email": "[email protected]",
           "appointment_date": "23July",
           "appointment_category": 4,
           "time": "afternoon"
            hasMultipleAppointmentOnDate: "nope"

            },{

           "ap_id": 44,
           "name": "John",
           "email": "[email protected]",
           "appointment_date": "23July",
           "appointment_category": 4,
           "time": "afternoon"
            hasMultipleAppointmentOnDate: "yes"

            },{
              ...

      }];
javascript underscore.js
2个回答
0
投票

您需要按组合键进行分组:

// data
const allAppointments = [
 {
        "ap_id": 23,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"    
   },
      {
        "ap_id": 55,
        "name": "Rose",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 70,
        "name": "Kate",
        "email": "[email protected]",
        "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>

1
投票

好吧,你不需要做所有这些分组和映射。您所要做的就是根据您检查的当前约会,单个地图和计数:

var allAppointments = [
 {
        "ap_id": 23,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 3,
        "time": "morning"    
   },
      {
        "ap_id": 55,
        "name": "Rose",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 44,
        "name": "John",
        "email": "[email protected]",
        "appointment_date": "23July",
        "appointment_category": 4,
        "time": "afternon"        
      },
      {
        "ap_id": 70,
        "name": "Kate",
        "email": "[email protected]",
        "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>
© www.soinside.com 2019 - 2024. All rights reserved.