与子阵列组阵 [英] group array with sub array

查看:116
本文介绍了与子阵列组阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到采取了类似信息的数组,并创建子阵列中的最简单的方法。新创建的父阵列还应该总结那些子阵列中找到的值。这是一个SQL语句,它通过使用金额/组相当。

这是原始数组

  [
    {
        用户id:1,
        用户名:鲍勃,
        '类':'鞋',
        计数:2
    },
    {
        用户id:1,
        用户名:鲍勃,
        '类':'岩石',
        计数:4
    },
    {
        用户id:1,
        用户名:鲍勃,
        '类':'袋',
        计数:3
    },
    {
        '用户id':2,
        用户名:'告',
        '类':'鞋',
        计数:1
    },
    {
        '用户id':2,
        用户名:'告',
        '类':'岩石',
        计数:7
    },
    {
        '用户id':2,
        用户名:'告',
        '类':'袋',
        计数:4
    },
]

这就是我要新创建的数组的样子:

  [
    {
        用户id:1,
        用户名:鲍勃,
        '购买':[
            {'类':'鞋','伯爵':'2'},
            {'类':'岩石','伯爵':'4'},
            {'类':'袋','伯爵':'3'}
        ]
        TOTALCOUNT':9
    },
    {
        '用户id':2,
        用户名:'告',
        '购买':[
            {'类':'鞋','伯爵':'1'},
            {'类':'岩石','伯爵':'7'},
            {'类':'袋','伯爵':'4'}
        ]
        TOTALCOUNT':12
    },
]


解决方案

首先 GROUPBY 用户id,然后地图 组以获得所需的结构:

  VAR组= _.groupBy(列表中,'用户id');    VAR的结果= _.map(组,功能(用户){
        返回{
            用户名:用​​户[0] .userId,
            用户名:用​​户[0] .userName,
            采购:_.map(用户,功能(购买){
                返回{
                    类别:purchase.category,
                    数:purchase.count
                }
            }),
            TOTALCOUNT:_.reduce(用户,功能(备忘录,购买){
                返回备忘录+ purchase.count;
            },0)
        }
    });

I'm trying to find the easiest way to take an array that has like information and create an array with sub array. The newly created parent array should also sum the values that are found in the sub-array. This is the equivalent of an SQL statement that uses sum/group by.

This is the original array

[
    {
        'userId':1,
        'userName':'bob',
        'category':'shoes',
        'count':2
    },
    {
        'userId':1,
        'userName':'bob',
        'category':'rocks',
        'count':4
    },
    {
        'userId':1,
        'userName':'bob',
        'category':'bags',
        'count':3
    },
    {
        'userId':2,
        'userName':'sue',
        'category':'shoes',
        'count':1
    },
    {
        'userId':2,
        'userName':'sue',
        'category':'rocks',
        'count':7
    },
    {
        'userId':2,
        'userName':'sue',
        'category':'bags',
        'count':4
    },
]

This is what I want the newly created array to look like:

[
    {
        'userId':1,
        'userName':'bob',
        'purchases': [
            {'category':'shoes','count':'2'},
            {'category':'rocks','count':'4'},
            {'category':'bags','count':'3'}
        ],
        'totalCount' : 9
    },
    {
        'userId':2,
        'userName':'sue',
        'purchases': [
            {'category':'shoes','count':'1'},
            {'category':'rocks','count':'7'},
            {'category':'bags','count':'4'}
        ],
        'totalCount' : 12
    },  
]

解决方案

First groupBy userId and then map the groups to get the desired structure:

    var groups = _.groupBy(list, 'userId');

    var result = _.map(groups, function(user){
        return {
            userId: user[0].userId,
            userName: user[0].userName,
            purchases: _.map(user, function(purchase){
                return {
                    category: purchase.category,
                    count: purchase.count
                }
            }),
            totalCount: _.reduce(user, function(memo, purchase){
                return memo + purchase.count;
            }, 0)
        }
    });

这篇关于与子阵列组阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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