简体   繁体   English

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

[英]With Crossfilter how do I return an array of all the id values for a specific type

Forgive me, I'm not sure I'm approaching this problem correctly. 原谅我,我不确定我是否正确地接近了这个问题。

I have some data (many thousands of elements) with a type and an ID: 我有一些类型和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 },
  ...
];

With crossfilter , I want to filter by a type and return an array of all their ids. 使用crossfilter ,我想按类型过滤并返回所有id的数组。

For example: all the type 'bar' should return [10, 11, 13, 17] 例如:所有类型'bar'应该返回[10, 11, 13, 17]

My attempt was to group reduce. 我的尝试是减少组。 But I didn't get very far with it: 但我对此并不是很了解:

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 = () => ([]);

And then something like: 然后是这样的:

d.group().reduce(reduceAdd, reduceRemove, reduceInitial)

You should use filter method in combination with map and destructing assignment . 您应该将filter方法与mapdestructing赋值结合使用。

 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)); 

What you've got looks pretty much correct with me. 你得到的东西对我来说非常正确。 You just have to query your group by saving it to a variable 您只需通过将其保存到变量来查询您的组

var grp = d.group().reduce(reduceAdd, reduceRemove, reduceInitial)

and then query it like 然后查询它

grp.top(Infinity)

This will return an array of objects. 这将返回一个对象数组。 The key of one of the objects will be bar and the value of that object will be the array of records where type is bar . 其中一个对象的键将是bar ,该对象的值将是typebar的记录数组。

Using a single forEach() is more efficient in this case instead of using filter() and then map() because you have complexity of O(n) where n is the number of objects but using filter() and then map() there will be complexity of O(n+m) where m is the number of filtered records on which you do map() : 在这种情况下使用单个forEach()更有效,而不是使用filter()然后使用map()因为你有O(n)复杂性,其中n是对象的数量,但使用filter()然后map()将是O(n+m)复杂性,其中m是您在其上执行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 }, ]; let type = 'bar'; var res = []; data.forEach((obj)=> { if(obj.type===type){ res.push(obj.id); } }); console.log(res); 

If there are 8 objects then you are iterating 8 times in filter and then lets say you get 4 records in filter you will then iterate 4 times to get the id value in the resultant array. 如果有8对象,那么你在过滤器中迭代8次然后让你说你在过滤器中得到4条记录,然后你将迭代4次以获得结果数组中的id值。 Total of 12 iterations. 12次迭代。 So, in such cases I prefer to support the usage of forEach() in which there will be only 8 iterations to get the same set of array. 因此,在这种情况下,我更愿意支持使用forEach() ,其中只有8次迭代才能获得相同的数组。

 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 }, ]; let type = 'bar'; let result = data.reduce((acc, {type, id})=>{ if(!acc[type]) acc[type]=[]; acc[type].push(id); return acc },{}) console.log(result[type]); 

You can also use Array.reduce here. 您也可以在这里使用Array.reduce

 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 } ]; const filteredArray = data.reduce((result, obj) => { if (obj.type === "bar") { result.push(obj.id) } return result; }, []); console.log(filteredArray) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM