Removing Specific Object In Array Based On Property?

前端 未结 1 731
我寻月下人不归
我寻月下人不归 2021-01-19 05:45

I have an array of drink toppings and want to remove the ones that aren\'t relevant to the drink, this is the code I have, but I can\'t figure out how to remove the topping

相关标签:
1条回答
  • 2021-01-19 06:40

    You can do in-place removal with a for loop, but it would be tricky, because you would need to iterate back to avoid disturbing indexes.

    A simpler approach is to filter the array, and assign it back to the toppings property, like this:

    toppings = toppings.filter {$0.limit != "C"}
    
    0 讨论(0)
提交回复
热议问题