有几个基于简单数组的示例,但是我有2个对象数组,似乎无法使它正常工作。
getAvailableApplications() : Application[]
{
var list;
if (!this.userSettings)
list = this.applications;
else
list =
this.applications.filter(x=>
this.userSettings.modules.filter(y=>
x.entityId === y.moduleId
));
return list;
}
这总是返回我完整的应用程序列表,而不是删除那些不在userSetting.modules数组中的应用程序,尽管我在userSettings.modules数组中有一个元素。
我还尝试了不带lambda的重写,但似乎无法识别模块级别的userSettings变量
this.applications.filter(function (x){
return this.userSettings.filter(function(y){
return x.entityId === y.moduleId;
})
userSettings被声明为任意,但分配了一个值,如
userSettings = {"settings":{"modules":[{"moduleId":"b37f590de59943278e7f9b2137c0c232",
"order":0}]}
应用程序的声明如下:
applications : Application[]
答案 0 :(得分:2)
如果我正确理解,这就是您要实现的目标,无需使用两个过滤器。
NetworkParameters
查看示例:
list = this.applications
.filter(x => this.userSettings.modules.map(y => y.moduleId).includes(x.entityId));
答案 1 :(得分:2)
这是您要完成的事情:
this.applications.filter( x =>
this.userSettings.some(y =>
x.entityId === y.moduleId;
)
)
“ some()方法测试数组中的至少一个元素是否通过了由提供的函数实现的测试。”
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some