下划线sortBy,groupBy和分组数组的数量 [英] Underscore sortBy, groupBy with count of grouped arrays

查看:103
本文介绍了下划线sortBy,groupBy和分组数组的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用下划线操作对象数组时遇到一些麻烦。我快到了,但在尝试将countBy合并到下划线链中时遇到了麻烦。

I'm having a bit of trouble manipulating an array of objects using underscore. I am almost there but have got stuck on attempting to combine countBy within the underscore chain.

我有以下对象数组(简化):

I have the following array of objects (simplified):

var messages = [
{
    id: 20,
    messageHeaderId: 6,
    content: "Message 1",
    dateOfMessage: "2017-08-16T12:34:49.403974",
    sender: "Diane"
},
{
    id: 22,
    messageHeaderId: 6,
    content: "Latest message from MH6",
    dateOfMessage: "2017-08-16T12:38:49.403974",
    sender: "Diane"
},
{
    id: 13,
    messageHeaderId: 5,
    content: "Message 1 from MH5",
    dateOfMessage: "2017-08-16T10:34:23.848459",
    sender: "Ora"
},
{
    id: 14,
    messageHeaderId: 5,
    content: "Latest message from MH5",
    dateOfMessage: "2017-08-16T12:37:12.237183",
    sender: "Ora"
},

{
    id: 15,
    messageHeaderId: 5,
    content: "Message 2 from MH5",
    dateOfMessage: "2017-08-16T11:00:35.547921",
    sender: "Ora"
},

{
    id: 16,
    messageHeaderId: 5,
    content: "Message 3 from MH5",
    dateOfMessage: "2017-08-16T11:36:51.828447",
    sender: "Ora"
}
];

我的目标是在每个线程中仅返回由messageHeaderId标识的最新消息,以及线程中作为新对象数组的消息计数。

My goal is to return just the latest message in each thread, identified by the messageHeaderId, along with the count of messages in the thread as a new array of objects.

因此,从上面的数组开始,这将导致2个对象组成的数组,这些对象具有groupBy方法之后每个组中邮件数量的count属性,并作为属性返回的最新消息。

So from the array above this would result in an array of 2 objects that have a count property of the number of messages in each group after the groupBy method and added as a property to the returned latest messages.

这是我用来对数组进行排序和分组的方法(我不确定是否有2个map函数是一个好主意(?),但这是我设法做到这一点的唯一方法):

This is the method I am using to sort and group the array (I'm not sure if having 2 map functions is a good idea(?) but this was the only way I managed to get this far):

var selectedMessages = _.chain(messages)
.sortBy('dateOfMessage')
.reverse()
.groupBy('messageHeaderId')
.map(function(a) {return a[0]}) // return latest message in thread
.map(function(b){
    return {
        content: b.content,
        dateOfMessage: b.dateOfMessage,
        headerId: b.messageHeaderId
        // count: ??
    }
})
.value();

console.log(selectedMessages);

这为我提供了线程中的最新消息,作为新的对象数组,我只是想念计算财产。我尝试了_.countBy的各种组合,但我不确定如何将其放入返回的数组中。

This gives me the latest message in the thread as a new array of objects and I am just missing the count property. I have tried various combinations of _.countBy I am just not sure how to get that into the returned array.

我在这里用上面的代码创建了一个jsfiddle: https ://jsfiddle.net/xsfs0ua0/

I have created a jsfiddle here with the above code: https://jsfiddle.net/xsfs0ua0/

推荐答案

您需要在<$ c $之后设置计数c> groupBy ,但在第一个映射完成之前,因为在该映射中,您仅返回一个记录

You need to set the count after the groupBy, but before the first map completes, because in that map you're returning only one record.

我修改了小提琴: https:/ /jsfiddle.net/xsfs0ua0/1/

以下是相关更改:

var selectedMessages = _.chain(messages)
    .sortBy('dateOfMessage')
    .reverse()
    .groupBy('messageHeaderId')
    .map(function(a) {
        // since returning only a[0], assign a new property 'count' with the size of the array
        a[0]['count'] = _.size(a);
        return a[0];
    })
    .map(function(b){...})
    .value();

这篇关于下划线sortBy,groupBy和分组数组的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆