使用Crossfilter,如何返回特定类型的所有id值的数组

Rim*_*ian 5 javascript crossfilter

原谅我,我不确定我是否正确解决了这个问题。

我有一些具有类型和ID的数据(成千上万个元素):

const data = [
  { type: 'foo', id: 1 },
  { type: 'foo', id: 3 },
  { type: 'foo', id: 5 },
  { type: 'baz', id: 8 },
  { type: 'baz', id: 10 },
  { type: 'bar', id: 11 },
  { type: 'bar', id: 13 },
  { type: 'bar', id: 17 },
  ...
];
Run Code Online (Sandbox Code Playgroud)

使用crossfilter,我想按类型过滤并返回其所有id的数组。

例如:所有“ bar”类型都应返回 [10, 11, 13, 17]

我的尝试是减少组。但是我并没有走太远:

let ndx = crossfilter(data);
let d = ndx.dimension(d => d.type);
let reduceAdd = (p, v) => p.push(v);
let reduceRemove = (p, v) => p.filter(i => i !== v);
let reduceInitial = () => ([]);
Run Code Online (Sandbox Code Playgroud)

然后类似:

d.group().reduce(reduceAdd, reduceRemove, reduceInitial)
Run Code Online (Sandbox Code Playgroud)

Mih*_*nut 5

您应该filter结合使用方法map破坏分配

const data = [ { type: 'foo', id: 1 }, { type: 'foo', id: 3 }, { type: 'foo', id: 5 }, { type: 'baz', id: 8 }, { type: 'baz', id: 10 }, { type: 'bar', id: 11 }, { type: 'bar', id: 13 }, { type: 'bar', id: 17 }, ], type = 'bar';
console.log(data.filter(elem => elem.type == type).map(({id}) => id));
Run Code Online (Sandbox Code Playgroud)