-1

I have an array of objects. Every object has a double value. Before using the list, I want to remove all objects with the value 0.0 from it, How to do it? As you can see in this example, the object with the PM10 key has the value 0.0. I want to delete this object and every entry with this value if the list changes.

dataEntries =
        [
        FavoriteData(id: 0, key: "NO2", values: [FavoriteValue(date: "", value: 40.0)]),
        FavoriteData(id: 0, key: "PM10", values: [FavoriteValue(date: "", value: 0.0)]),
        FavoriteData(id: 0, key: "PM2.5", values: [FavoriteValue(date: "", value: 55.0)]),
        FavoriteData(id: 0, key: "O3", values: [FavoriteValue(date: "", value: 17.0)]),
        FavoriteData(id: 0, key: "NO2", values: [FavoriteValue(date: "", value: 40.0)]),
        FavoriteData(id: 0, key: "C6H6", values: [FavoriteValue(date: "", value: 10.0)])
        ]
0

1 Answer 1

1

There is removeAll(where:) in Array which removes all items which match the predicate

dataEntries.removeAll{ data -> Bool in
    data.values.contains{$0.value == 0.0}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! Works very well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.