How to remove multiple items from a swift array?

Raffi

For example i have an array

var array = [1, 2, 3, 4]

I want to remove item at index 1 then at index 3 "let it be in a for loop".

But removing the item at index 1 will move the item at index 3 to index 2, thus messing up the second removal.

Any suggestions ?

Luca Angeletti

Given your array

var numbers = [1, 2, 3, 4]

and a Set of indexes you want to remove

let indexesToRemove: Set = [1, 3]

You want to remove the values "2" and "4".

Just write

numbers = numbers
    .enumerated()
    .filter { !indexesToRemove.contains($0.offset) }
    .map { $0.element }

Result

print(numbers) // [1, 3]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove multiple items from a swift array?

From Dev

AngularFire how to remove multiple items from array?

From Dev

How to remove multiple items from an array via looping in Javascript

From Dev

How to remove multiple items from an array via looping in Javascript

From Dev

How to remove multiple sequential items of an array - JavaScript

From Dev

How to remove multiple items in an array of objects in React?

From Dev

How to remove items from an array of objects?

From Dev

Remove items from array

From Dev

How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

From Dev

How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

From Dev

IBM Watson Assistant, how can I remove multiple items from an array?

From Dev

Remove Multiple Items from Array using underscore.js?

From Dev

Remove multiple items from array based on matched value

From Dev

Remove multiple items from a slice

From Dev

Remove multiple items from ExpandableListView

From Java

How to remove an element from an array in Swift

From Dev

How to remove common items from two struct arrays in Swift

From Dev

swift: how to remove randomly from 1 to 3 items?

From Dev

How to remove multiple values from an array at once

From Dev

Is it possible to remove items from a array?

From Dev

How to remove multiple items from a list, not just access them

From Dev

How could I remove the duplicated items(complex object) from array

From Dev

How do I safely remove items from an array in a for loop?

From Dev

How do I remove/filter specific items from an array?

From Dev

How to remove items with certain length from array in php

From Dev

How to deal with multiple items' states from FlastList( How to setState multiple items from the Array?)

From Dev

KnockoutJS: Remove array of items from an array

From Dev

Remove items from one array if not in the second array

From Dev

KnockoutJS: Remove array of items from an array

Related Related

  1. 1

    How to remove multiple items from a swift array?

  2. 2

    AngularFire how to remove multiple items from array?

  3. 3

    How to remove multiple items from an array via looping in Javascript

  4. 4

    How to remove multiple items from an array via looping in Javascript

  5. 5

    How to remove multiple sequential items of an array - JavaScript

  6. 6

    How to remove multiple items in an array of objects in React?

  7. 7

    How to remove items from an array of objects?

  8. 8

    Remove items from array

  9. 9

    How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

  10. 10

    How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

  11. 11

    IBM Watson Assistant, how can I remove multiple items from an array?

  12. 12

    Remove Multiple Items from Array using underscore.js?

  13. 13

    Remove multiple items from array based on matched value

  14. 14

    Remove multiple items from a slice

  15. 15

    Remove multiple items from ExpandableListView

  16. 16

    How to remove an element from an array in Swift

  17. 17

    How to remove common items from two struct arrays in Swift

  18. 18

    swift: how to remove randomly from 1 to 3 items?

  19. 19

    How to remove multiple values from an array at once

  20. 20

    Is it possible to remove items from a array?

  21. 21

    How to remove multiple items from a list, not just access them

  22. 22

    How could I remove the duplicated items(complex object) from array

  23. 23

    How do I safely remove items from an array in a for loop?

  24. 24

    How do I remove/filter specific items from an array?

  25. 25

    How to remove items with certain length from array in php

  26. 26

    How to deal with multiple items' states from FlastList( How to setState multiple items from the Array?)

  27. 27

    KnockoutJS: Remove array of items from an array

  28. 28

    Remove items from one array if not in the second array

  29. 29

    KnockoutJS: Remove array of items from an array

HotTag

Archive