JSFiddle - React, Tailwind, and code Playground

by Andrey Gershun

HTML

<script src="http://alasql.org/console/alasql.min.js"></script>
<span id="res"></span>

JavaScript

var data = [
    {
        '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
    },
];

    var res = alasql('SELECT userId, FIRST(userName) AS userName, ARRAY({category:category,[count]:[count]}) AS purchases, SUM([count]) AS totalCount FROM ? GROUP BY userId, userName',[data]);
 
document.getElementById("res").textContent = JSON.stringify(res);