javascript - 向分组对象添加属性并取消分组

标签 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"

            },{
              ...

      }];

最佳答案

嗯,您不需要进行所有这些分组和映射。您所要做的就是一张 map 和基于您检查的当前约会的计数:

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>

关于javascript - 向分组对象添加属性并取消分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54999039/

相关文章:

javascript - 安装 Firefox 插件问题

javascript - 使用 'this' 引用与meteor.template.rendered

javascript:为什么需要删除数组的 0 索引

javascript - 将平面嵌套数据转换为单个数组

javascript - 如何一次将一个元素多次添加到数组中

javascript - 无法在同一台机器上加载 XMLHttpRequest

javascript - JS : Change select option value on being selected with prompt?

javascript - 下划线模板无法访问变量

javascript - 试图找到一种使用函数式编程风格转换此数组的漂亮方法

javascript - 在 ._contains(arr, val); 中使用正则表达式