How do I filter an array based on object's property with multiple OR statements

前端 未结 1 611
面向向阳花
面向向阳花 2021-01-24 07:37

The question was difficult to put into words, but here is my situation. I have several Monster objects in an array called monsters. Each monster has a name

相关标签:
1条回答
  • 2021-01-24 08:03

    You could do something like:

    let monsters: [Monster] = ...
    
    let monsterNames: [String] = ...
    
    let filteredMonsters = monsters.filter { monsterNames.contains($0.name) }
    

    This doesn't perform all that well, since it will go over the names array up to n times for each monster, but if your names arrays is small, this won't be a problem.

    0 讨论(0)
提交回复
热议问题