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

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

问题描述

这是我第一次尝试使用一些JSON数据对象来完成JavaScript,并且需要一些建议来正确实现我的目标。



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

  var dataString ='$ DATASTRING $' ; 

但是最终的结果是我必须在服务器替换其数据之后使用r \ n,当然):

  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:搜索引擎,hit:6,bytes:
{category:Search Engines,hits:1,bytes:2858}
'';

然后,我可以将其更改为一个对象来处理。

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

这允许我访问各个数据行的数据,但是我需要求和,并命令值。

我需要这样的SQL语句的等价物:

  SELECT类别,sum(hits),sum(bytes)
FROM dataObject
GROUP BY类别
ORDER BY sum(字节)DESC

我希望的输出是一个像这样的对象,我可以进一步处理:

<$ 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}
'';

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



我可以遍历所有的类别值并首先找到唯一的类别,然后再次循环并总结命中和字节,然后再次排序,但看起来似乎有一个更简单的方法。 / p>

prototype.js(1.7)已包含在客户端页面上,但如果必须的话,我可以添加Underscore,jQuery或其他小型库。



我只是不知道用最少的代码处理查询会是最好的,最简单的,最小的。



任何建议?

解决方案

您可以使用本地函数 .reduce()聚合数据,然后 .sort()字节进行排序。

  var result = dataObject.reduce(function(res,obj){
if(!(obj.category 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;});

如果您支持较早的实现,则需要使用 shim for .reduce()


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.

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 \r\n, 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.

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) 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.

Any suggestions?

解决方案

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; });

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

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

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