我在https://jsfiddle.net/wheatgrass/93w6p6dc/6/上发了个帖子
(但在运行can时遇到问题),您(希望)可以看到数据和我的交叉过滤器输出。
最初,我将数据放在一个嵌套数组中,但是我读到cf需要“扁平化”数据,所以我添加了扁平化数据的两个子集示例,countries1和countries2。我想使用countries2。
我尝试使用了reductio帮助器库(耶!)通过键"nid“和一个带有值的国家数组获得要输出的n1dimgroup。对于我正在尝试的东西,我在SO或其他地方找不到任何示例。
我如何重写reducer,使n1dimgroup输出是一个带有键、值的对象,如下所示?
{ key: Benin, value: 1}
{ key: Nepal, value: 2}
...等?
谢谢。我已经缩短了下面的代码,但数据在jfiddle上。
下面的代码
<script>
//please see var countries2 on the jfiddle
var countries = [
"key":"Albania",
"value":2,
"key":"Jamaica",
"value":1,
"key":"Senegal",
"value":3,
"key":"Morocco",
"value":2,
"nid":"0",
},
{
"key":"Fiji",
"value":1,
"key":"Mongolia",
"value":1,
"key":"Uganda",
"value":1,
"nid":"36",
}
var n1 = crossfilter(countries2);
var n1dim = n1.dimension(function(d) { return d.nid});
var n1dimgroup = n1dim.group();
console.log("n1dim",n1dim.top(Infinity));
console.log("n1dimgroup",n1dimgroup.top(Infinity));
var reducer = reductio()
.exception(function(d) { return d.key; })
.exceptionCount(true)
.exceptionSum(function(d) { return d.value; });
reducer(n1dimgroup);
console.log('reducer', reducer);
console.log('n1dimgrouptop after reductio', n1dimgroup.top(Infinity));
console.log('n1dimgrouptop.key', n1dimgroup.top(Infinity).key);
console.log('n1dimgrouptop.value', n1dimgroup.top(Infinity).value);
</script>如何让reducer在对象中以键、值的形式提供输出?
发布于 2016-04-18 23:41:49
Ethan的示例fiddle向我展示了在对组使用reductio lib之后如何使用访问器函数。这回答了我的问题。谢谢!
https://stackoverflow.com/questions/36669246
复制相似问题