Crossfilter query

后端 未结 2 822
太阳男子
太阳男子 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:29

    I've never used "crossfilter" (I'm assuming this is a JS library). Here are some pure JS methods though.

    This...

    data.filter(function(d) {
      return d.authors.indexOf("Michael Fogus") !== -1;
    })
    

    returns this:

    [{bookname:"the joy of clojure", authors:["Michael Fogus", "Chris Houser"], tags:["clojure", "lisp"]}]
    

    This...

    var res = {};
    data.forEach(function(d) {
      d.tags.forEach(function(tag) {
        res.hasOwnProperty(tag) ? res[tag]++ : res[tag] = 1
      });
    })
    

    returns this:

    ({clojure:1, lisp:1, ruby:2, 'design patterns':1})
    

    To either of these, you can apply d3.entries to get your {key:"ruby", value: 2} format.

提交回复
热议问题