Working from "I have to filter out the condition such that if filter.fieldId is not present in the contactCustomField then remove the filter and if in case in the registrationCondition there was only one filter which got removed and then remove that condition object from registrationConditions itself. "(从“我必须过滤条件,如果在contactCustomField中不存在filter.fieldId的情况下,则删除该过滤器,并且如果在registrationCondition中只有一个过滤器被删除,然后从registrationConditions自身中删除该条件对象“)
first, let's just look at all the filters from the registrationConditions
:(首先,让我们看一下registrationConditions
中的所有过滤器:)
filters = registrationConditions.map(x => x.filters)
right so, now, remove the ones that are not in contactCustomFields
:(正确,现在,删除不在contactCustomFields
中的那些:)
filters = registrationConditions.map(x => x.filters)
.filter(x = > contactCustomFields.includes(x.fieldId))
At this point, we know how to process the filters property of each array entry but we also want to modify the array.(至此,我们知道了如何处理每个数组条目的filters属性,但是我们也想修改数组。) To do that we have to work from the array:(为此,我们必须从数组开始工作:)
registrationConditions = registrationConditions
.map(condition => {
filters = condition.filters.filter(f => contactCustomFields.includes(f.fieldId))
if (condition.filters.length === filters.length + 1) {
return null
}
condition.filters = filters
return condition
})
.filter((obj) => obj /* remove those nulls */)
This might lack a bit of polish (didnt actually test it against your objects), but outside of typos and maybe some elementary stuff, I think this is rendering your intent.(这可能没有一点点修饰(实际上没有针对您的对象进行测试),但是除了错别字和一些基本的东西之外,我认为这正在表达您的意图。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…