[英]Advanced Array.prototype.filter with Javascript
I have an Javascript object like so... 我有一个像这样的Javascript对象...
var strategies = [{
"strategy": {
"category": "war"
}
}, {
"strategy": {
"category": "farming"
}
}]
I then have an array that indicates which results I'd like back. 然后,我有一个数组,指示我想要返回的结果。 It can be any of the following: []
OR ["war"]
["farming"]
OR ["war", "farming"]
. 它可以是以下任意一项: []
OR ["war"]
["farming"]
OR ["war", "farming"]
。
If we have the []
, I want to return no results. 如果我们有[]
,我想不返回任何结果。 But if ["war", "farming"]
I want to return both of the results above. 但是如果["war", "farming"]
我想返回上面的两个结果。
How do I accomplish this with Array.prototype.filter ? 如何使用Array.prototype.filter完成此操作? I saw this post , but couldn't reason through it. 我看到了这篇文章 ,但无法推理。
strategies.filter((strategy) =>
????
)
Thanks for your help. 谢谢你的帮助。
Your object, strategy was a wrapped object, so my first line was setting it to its inner strategy and then filter as needed. 您的对象strategy是包装对象,因此我的第一行将其设置为内部策略,然后根据需要进行过滤。
var strategies = [{
"strategy": {
"category": "war"
}
}, {
"strategy": {
"category": "farming"
}
}]
var b = ["war", "farming"];
strategies.filter(function(strategy){
strategy = strategy.strategy;
for(var i in b){
if (b[i] == strategy["category"]) {
return true;
}
}
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.