[英]filter an array of objects by an array attribute with another array
The following code does NOT return the expected results of an array with 1 item. 以下代码不返回包含1个项目的数组的预期结果。 Can someone please enlighten me about the correct syntax?
有人可以启发我正确的语法吗?
let things = [{ Name: 'Bill', Tags: ['tall', 'dude'] }, { Name: 'Ted', Tags: ['short', 'dude'] } ]; let selectedTags = ['short', 'chick']; let filtered = things.filter((thing => thing.Tags.indexOf(selectedTags) >= 0)); console.log(filtered);
You are passing Array selectedTags
to includes
. 您正在将Array
selectedTags
传递给includes
。 You need to check loop through selectedTags
and check if tag is present in .Tags
of object. 您需要检查通过
selectedTags
循环,并检查对象的.Tags
中是否存在标签。
let things = [{Name: 'Bill', Tags: ['tall','dude']}, {Name: 'Ted', Tags: ['short','dude']}]; console.log(things); let selectedTags = ['short','chick']; let filtered = things.filter(x => { for(let tag of selectedTags){ if(x.Tags.includes(tag)) return true; } return false; }); console.log(filtered);
selectedTags
is an array. selectedTags
是一个数组。
.indexOf()
is wainting for a string. .indexOf()
正在等待字符串。
You can get the string into your array by selected with index, like : 您可以通过使用index选择将字符串放入数组,例如:
thing.Tags.indexOf(selectedTags[0])
So it looks like you want to filter things
based on if any of thing.Tags
is in selectedTags
. 因此,它看起来像你想过滤
things
基于如有的thing.Tags
是selectedTags
。
You would do something like this 你会做这样的事情
things.filter(thing => thing.Tags.some(tag => selectedTags.includes(tag)));
This is, I think, the most expressive way to write it. 我认为,这是最有表现力的书写方式。 Give me all things where some of thing.Tags are included in the selectedTags array.
给我所有东西,其中某些东西包含在selectedTags数组中。
If you need to filter the items that have some tag
that is present on the selectedTags
array, then you can go with this: 如果您需要过滤在
selectedTags
数组中具有某些tag
的项目,则可以这样做:
let things = [ {Name: 'Bill', Tags: ['tall', 'dude']}, {Name: 'Ted', Tags: ['short', 'dude']} ]; let selectedTags = ['short', 'chick']; let filtered = things.filter( thing => selectedTags.some(x => thing.Tags.includes(x)) ); console.log(filtered);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
If you need to filter the items that have all the tags
present on the selectedTags
array, then you can use: 如果需要过滤在
selectedTags
数组上具有所有tags
的项目,则可以使用:
let things = [ {Name: 'Bill', Tags: ['tall', 'dude']}, {Name: 'Ted', Tags: ['short', 'dude']}, {Name: 'Test', Tags: ['short', 'chick']} ]; let selectedTags = ['short', 'chick']; let filtered = things.filter( thing => selectedTags.every(x => thing.Tags.includes(x)) ); console.log(filtered);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
You have to iterate over each things
element and analyze every item in its Tags
attribute array: 你必须遍历每个
things
元素,并在其每一项分析Tags
属性数组:
let things = [{ Name: 'Bill', Tags: ['tall', 'dude'] }, { Name: 'Ted', Tags: ['short', 'dude'] } ]; let selectedTags = ['short', 'chick']; let filtered = things.filter(el => { //Use reduce on to get a count of how many matching tags return el.Tags.reduce((accum, tag) => { if (selectedTags.indexOf(tag) > -1) { accum += 1; } return accum; }, 0); }); console.log(filtered);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.