0
arrayOfTuples = [(4, 4, "id1"), (3, 6, "id2"), (3, 6, "id3")]

How to remove the item with the id2 string?

1 Answer 1

4

You can use RangeReplaceableCollection method removeAll(where:) and pass a predicate:

var arrayOfTuples = [(4, 4, "id1"), (3, 6, "id2"), (3, 6, "id3")]
arrayOfTuples.removeAll(where: {$2 == "id2"})
print(arrayOfTuples)  // [(4, 4, "id1"), (3, 6, "id3")]

If you would like to remove only the first occurrence where the third element of your tuple is equal to "id2" you can use Collection's method firstIndex(where:):

if let index = arrayOfTuples.firstIndex(where: {$2 == "id2"}) {
    arrayOfTuples.remove(at: index)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.