Search code examples
crossfilter

Crossfilter grouping filtered keys


I have some json, for examle:

data = {
         "name":"Bob","age":"20",
         "name":"Jo","age":"21",
         "name":"Jo","age":"22",
         "name":"Nick","age":"23"
       }

Next, I use crossfilter, create dimension and filter it:

let ndx = crossfilter(data); 
let dim = ndx.dimension(d => d.name).filter(d !== "Jo");
//try to get filtered values
let filtered = dim.top(Infinity); // -> return 2 values where 'name'!='Jo'
//"name":"Bob","age":"20"
//"name":"Nick","age":"23"

let myGroup = dim.group(d => {
    if(d === 'Jo') {
      //Why we come here? This values must be filtered already
    }
})

How can I filter my dimension and don't have these values on 'dim.group'?


Solution

  • Not sure what version you are using, but in the current version of Crossfilter, when a new group is created all records are first added to the group and then filtered records are removed. So the group accessor will be run at least once for all records.

    Why do we do this? Because for certain types of grouping logic, it is important for the group to "see" a full picture of all records that are in scope.

    It is possible that the group accessor is run over all records (even filtered ones) anyway in order to build the group index, but I don't remember.