Crossfilter query

后端 未结 2 815
太阳男子
太阳男子 2021-02-01 03:42

Is it possible to filter a crossfilter dataset which has an array as the value?

For example, say I have the following dataset:

var data = [
  {
    book         


        
2条回答
  •  梦如初夏
    2021-02-01 04:22

    I've added comments to the code below. Big picture: use reduce function.

    var data = ...
    var filtered_data = crossfilter(data);
    var tags = filtered_data.dimension(function(d) {return d.tags});
    
    tags.groupAll().reduce(reduceAdd, reduceRemove, reduceInitial).value()
    

    Notice how I've used groupAll() instead of group() b/c we want our reduce functions (defined below) to operate on one group rather than 3 groups.

    Now the reduce functions should look like this:

    /*
     v is the row in the dataset
    
     p is {} for the first execution (passed from reduceInitial). 
     For every subsequent execution it is the value returned from reduceAdd of the prev row
    */
    function reduceAdd(p, v) {
      v.tags.forEach (function(val, idx) {
         p[val] = (p[val] || 0) + 1; //increment counts
      });
      return p;
    }
    
    function reduceRemove(p, v) {
       //omitted. not useful for demonstration
    }
    
    function reduceInitial() {
      /* this is how our reduce function is seeded. similar to how inject or fold 
       works in functional languages. this map will contain the final counts 
       by the time we are done reducing our entire data set.*/
      return {};  
    }
    

提交回复
热议问题