# What is the best way to delete an object from a filtered array?

**URL:** <https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470>\
**Category:** Swift\
**Created:** [January 10, 2018, 3:10pm UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470 "2018-01-10T15:10:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 10, 2018, 3:10pm UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/1 "2018-01-10T15:10:44Z")

</div>

I have a search bar that allows me to filter through an array. Once I have the results of the filter I would like to be able to delete a object, if necessary. Currently when I try to delete a search result it removes it from the searchResults array, but not the items array. I assume there is a way to do this, maybe by tracking the originating index? What is the best approach to this situation. Thank you.

items = [“Tom”, “Bill”, “Ed”, “Joe”] (Ed is at Index 2 in this array)

searchResults = [“ED”] (Ed is at index 0 in this array)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 11, 2018, 1:13am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/2 "2018-01-11T01:13:54Z")

</div>

Hi @jport1130,  
Does your array have unique items? If so then from the filtered array you can get the value you want to remove from the array and remove it using the index(of:) method on array.

`let itemsToRemove = "Ed"`

```
if let index = items.index(of: itemToRemove) {
    items.remove(at: index)
}

```

Hope that helps,

Jayant

---

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 11, 2018, 2:26am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/3 "2018-01-11T02:26:03Z")

</div>

Hi Jayant,

This is the code I currently have:

@IBAction func deleteSelected() {  
if let selected = collectionView.indexPathsForSelectedItems {  
let items = selected.map{$0.item}.sorted().reversed()  
for item in items {  
print(item)  
estimateList.takeOffList.remove(at: item)  
}  
collectionView.deleteItems(at: selected)  
navigationController?.isToolbarHidden = true  
}  
}

My array does have unique items. When I filter the primary array the filtered array indexes change so when I try to delete my app crashes since the indexes are no longer the same. I hope this makes sense.

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 11, 2018, 6:20am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/4 "2018-01-11T06:20:51Z")

</div>

Hi John (@jport1130),  
that is too easy to fix and a simple mistake that many make.

The code is first removing the items from the array and then trying to remove it from the collectionView. Try to first remove the items in selected from the collectionView and then remove the items from the array.

Hope that resolves your problem,

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 11, 2018, 8:14pm UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/5 "2018-01-11T20:14:07Z")

</div>

Hi Jayant,

The code works on the “non-filtered” array. Once I filter the array and lets say delete index[0] of the filtered array my app crashes as this same item may have been index[3] in the non-filtered array.

I will try to get more information from my app for you.

Thanks again!

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 12, 2018, 1:49am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/6 "2018-01-12T01:49:55Z")

</div>

Hi john (@jport1130 ),  
yes that is how it is supposed to work.

Say you have 10 books (items in your array), your friend marks(selects) upto 4 books that they would like to read, so you have the index of the books they want. However one of that book is what you are wanting to read yourself, so you cannot give them that book as of now. So you want to remove that book from their selection and then give them the books they selected.

The only array (ownership) that you will modify here is your own, the master list, the one that caters as the source of all data.

Now, the problem here is that since the book you are reading is in index 8, you are trying to remove the book with the index #8 from your friends selection list which has only 4 items.

If you look at the first response with the sample code I send, I am getting the index of the item using the `index(of:)` function and passing the text of the item, in this example, using the title of the book from your friends list. That way you will get an index value which is between 0-3 and will not crash as opposed to getting a value between 0-9 which has the potential to crash or provide incorrect results.

Hope that helps,

Jayant

---

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 12, 2018, 5:05am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/7 "2018-01-12T05:05:34Z")

</div>

Thanks Jayant. I understand what you are saying,. I always struggle on implementation of the code you suggested into my app. I appreciate ALL the help!

---

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 13, 2018, 6:05am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/8 "2018-01-13T06:05:55Z")

</div>

Hi Jayant,

What other information can I send you to help me with this? I am at a road block and cannot get this to work for some reason (inexperiene 🙃)

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [January 13, 2018, 12:05pm UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/9 "2018-01-13T12:05:51Z")

</div>

Hi John,  
Post the code somewhere from where I can have a look. If you could post the entire project it would be helpful to see it with all of its dependencies (if any to run the same)

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![jport1130](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jport1130/32/102075_2.png) [@jport1130](https://forums.kodeco.com/u/jport1130)\
**Post date:** [January 14, 2018, 2:33am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/10 "2018-01-14T02:33:34Z")

</div>

Hi Jayant,

I am not sure how 🤔, but I figured it out with your original response. Thanks again for ALL your help!!! Onto the next challenge.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [June 26, 2018, 7:16am UTC](https://forums.kodeco.com/t/what-is-the-best-way-to-delete-an-object-from-a-filtered-array/38470/11 "2018-06-26T07:16:21Z")

</div>

Closed on June 26, 2018, 7:16am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
