JSON 数据的 JavaScript SUM 和 GROUP BY [英] JavaScript SUM and GROUP BY of JSON data

查看:34
本文介绍了JSON 数据的 JavaScript SUM 和 GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试使用一些 JSON 数据对象执行 JavaScript,需要一些有关实现目标的正确方法的建议.

This is my first attempt at doing JavaScript with some JSON data objects and need some advice on the proper way to attain my goal.

一些服务器端代码实际上生成了一个 JSON 格式的字符串,我必须使用它并将其分配给一个字符串:

Some server-side code actually generates a JSON formatted string that I have to work with and assign it to a string:

var dataString='$DATASTRING$';

但是在服务器替换其数据后我必须处理的最终结果(当然没有 ):

But the end-result I have to work with after the server substitutes its data (without the , of course):

var dataString='[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
]';

然后我可以将其更改为要使用的对象.

And then I can change it to an object to work with.

var dataObject=eval("("+dataString+")");

这允许我访问数据的各个数据行,但我需要对值进行求和、分组和排序.

This allows me to access the data individual rows of data, but I need to sum, group by, and order the values.

我需要像这样的 SQL 语句:

I need to the equivalent of an SQL statement like this:

SELECT category, sum(hits), sum(bytes) 
FROM dataObject
GROUP BY category
ORDER BY sum(bytes) DESC

我想要的输出是这样的对象,我可以进一步处理:

My desired output would be an object like this that I can further process:

var aggregatedObject='[ 
 { "category" : "Search Engines", "hits" : 13, "bytes" : 673684 },
 { "category" : "Content Server", "hits" : 3, "bytes" : 88930 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 } 
]';

...但我不知道从哪里开始.

...but i don't know where to start.

我可以遍历所有类别值并首先找到唯一的类别,然后再次循环并对命中和字节求和,然后再次进行排序,但似乎必须有更简单的方法.

I could loop through all the category values and find the unique categories first, then loop again and sum the hits and bytes, then again to sort, but it seems there has got to be an easier way.

prototype.js (1.7) 已经包含在客户端页面中,但如果需要,我可以添加 Underscore、jQuery 或其他一些小型库.

prototype.js (1.7) is already included on the client page, but I could add Underscore, jQuery, or some other small library if I had to.

我只是不知道用最少的代码来处理查询是最好的、最简单的、最小的.

I just don't know what would be best, easiest, smallest with the least amount of code to process the query.

有什么建议吗?

推荐答案

你可以使用原生函数 .reduce() 来聚合数据,然后 .sort()code> 按字节排序.

You can use the native functions .reduce() to aggregrate the data, and then .sort() to sort by bytes.

var result = dataObject.reduce(function(res, obj) {
    if (!(obj.category in res))
        res.__array.push(res[obj.category] = obj);
    else {
        res[obj.category].hits += obj.hits;
        res[obj.category].bytes += obj.bytes;
    }
    return res;
}, {__array:[]}).__array
                .sort(function(a,b) { return b.bytes - a.bytes; });

如果您支持较旧的实现,则需要使用 .reduce() 的垫片.

If you're supporting older implementations, you'll need to use a shim for .reduce().

这篇关于JSON 数据的 JavaScript SUM 和 GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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