简体   繁体   English

使用some()根据多个数组过滤一个数组

[英]filter an array based on multiple arrays using some()

It's quite possible I am going about this the wrong way, but I have a primary array that i need to filter if any of it's objects values exist in two other arrays. 我很有可能会以错误的方式进行处理,但是我有一个主数组,如果它的任何对象值都存在于另外两个数组中,则需要过滤该主数组。 I am trying to use a combination of filter() and some() but what I have right now is not working. 我正在尝试使用filter()some()的组合,但是我现在无法使用。

const milestones = <FormArray>this.piForm.get('_milestones');
if (this.piById) {
    milestonesToCreate = milestones.value
        .filter(milestone => !this.piById.milestones.some(item => item.milestoneId === milestone.milestoneId));
    milestonesToDelete = this.piById.milestones
        .filter(milestone => !milestones.value.some(item => item.milestoneId === milestone.milestoneId));
    milestonesToUpdate = milestones.value
        .filter(milestone => milestones.value
            .some(item =>
                item.milestoneId === milestonesToCreate.milestoneId && milestonesToDelete.milestoneId));
}

In the code above milestonesToUpdate should be a the filtered results where the array consists of objects that are not in milestonesToCreate and milestonesToDelete 在上面的代码中milestonesToUpdate应该是经过过滤的结果,其中数组由不在milestonesToCreatemilestonesToDelete的对象组成

Hopefully I've explained this well enough. 希望我已经解释得足够好了。

ADDED SAMPLE MILESTONES ARRAY 添加示例里程碑数组

milestones = [
  {
    "milestoneId": 0
  }
]

Firstly, it looks like your problem is just a misunderstanding of boolean checks in your final call to some() . 首先,看起来您的问题只是对您对some()最终调用中布尔检查的误解。

You have put: 您已输入:

item.milestoneId === milestonesToCreate.milestoneId && milestonesToDelete.milestoneId

Which is the same as saying, where item.milestoneId equals milestonesToCreate.milestoneId AND milestonesToDelete.milestoneId exists . 这与说相同, 其中item.milestoneId等于MilestonesToCreate.milestoneId,而MilestonesToDelete.milestoneId存在 I expect that you are just trying to check if the current value exists in both arrays. 我希望您只是尝试检查两个数组中是否都存在当前值。

it's better to achieve that in single pass: 最好一次通过即可实现:

  1. put all elements to elementsToUpdate , copy all elements from your this into elementsToDelete 把所有元素elementsToUpdate ,从您的副本的所有元素thiselementsToDelete
  2. iterate through elementsToUpdate , once some item does not exist in another list, move that element into elementsToCreate 遍历elementsToUpdate ,一旦另一个列表中不存在某项,则将该元素移到elementsToCreate
  3. if element exists in both, remove it from elementsToDelete . 如果两个元素都存在,则将其从elementsToDelete删除。
    1. finally you will get 3 lists you need. 最终,您将获得3个需要的列表。

And you can even speed up code more if instead of using arrays you use hash(old good {} ) where id are used as keys. 而且,如果使用id用作键的hash(old good {} )而不是使用数组,甚至可以提高代码的速度。 Then check "if element is here" would be as easy as item in elementsToUpdate instead of iterating all the elements each time 然后检查“如果元素在这里”将与item in elementsToUpdate一样容易,而不是每次都迭代所有元素

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

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