Alasql http://github.com/agershun/alasqlJavaScript SQL 库是专门为此类任务设计的:
<script src="alasql.min.js"></script>
<script>
var groups = [{id:4, name:"abcd", id_group:"1"},
{id:5, name:"efgh", id_group:"1"},
{id:6, name:"ijkl", id_group:"1"},
{id:4, name:"abcd", id_group:"2"},
{id:7, name:"mnop", id_group:"2"}];
var res = alasql('select id_group, count(id) as cnt from ? \
where id in (4,7) group by id_group having cnt = 2',[groups]);
</script>
你可以试试这个例子 http://jsfiddle.net/agershun/5prbwtzy/1/在 jsFiddle 中。
我修改了一点 SQL 表达式,因为 Alasql 不支持 HAVING 子句中的聚合函数(如 COUNT、SUM、MAX、MIN)。