如何从swift数组中删除多个项目?

时间:2022-01-14 21:21:16

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 any suggestions ?

我想删除索引1处的项目然后在索引3“让它在for循环中”但是删除索引1处的项目会将索引3处的项目移动到索引2任何建议吗?

3 个解决方案

#1


20  

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".

您想要删除值“2”和“4”。

Just write

写吧

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

Result

结果

print(numbers) // [1, 3]

#2


9  

It's simple. delete items from the end.

这很简单。从最后删除项目。

First delete 3 and after that delete 1

首先删除3,然后删除1

#3


0  

Swift 3: Use swift closure to perform the same operation.

Swift 3:使用swift闭包执行相同的操作。

If your array is like

如果你的阵列是这样的

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

and indexes you want to remove

和要删除的索引

let indexesToBeRemoved: Set = [2, 4]

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

print(numbers) // [0, 1, 3, 5]

打印(数字)// [0,1,3,5]

Swift 3: Here is same operation with JSON Object (dictionary)

Swift 3:这里是与JSON Object(字典)相同的操作

var arrayString = [
    [ "char" : "Z" ],
    [ "char" : "Y" ],
    [ "char" : "X" ],
    [ "char" : "W" ],
    [ "char" : "V" ],
    [ "char" : "U" ],
    [ "char" : "T" ],
    [ "char" : "S" ]
]

let arrayIndex = [2, 3, 5]

arrayString = arrayString.enumerated()
    .filter { !arrayIndex.contains($0.0 + 1) }
    .map { $0.1 }

print(arrayString)

[["char": "Z"], ["char": "W"], ["char": "U"], ["name": "T"], ["name": "S"]]

[[“char”:“Z”],[“char”:“W”],[“char”:“U”],[“name”:“T”],[“name”:“S”] ]

#1


20  

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".

您想要删除值“2”和“4”。

Just write

写吧

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

Result

结果

print(numbers) // [1, 3]

#2


9  

It's simple. delete items from the end.

这很简单。从最后删除项目。

First delete 3 and after that delete 1

首先删除3,然后删除1

#3


0  

Swift 3: Use swift closure to perform the same operation.

Swift 3:使用swift闭包执行相同的操作。

If your array is like

如果你的阵列是这样的

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

and indexes you want to remove

和要删除的索引

let indexesToBeRemoved: Set = [2, 4]

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

print(numbers) // [0, 1, 3, 5]

打印(数字)// [0,1,3,5]

Swift 3: Here is same operation with JSON Object (dictionary)

Swift 3:这里是与JSON Object(字典)相同的操作

var arrayString = [
    [ "char" : "Z" ],
    [ "char" : "Y" ],
    [ "char" : "X" ],
    [ "char" : "W" ],
    [ "char" : "V" ],
    [ "char" : "U" ],
    [ "char" : "T" ],
    [ "char" : "S" ]
]

let arrayIndex = [2, 3, 5]

arrayString = arrayString.enumerated()
    .filter { !arrayIndex.contains($0.0 + 1) }
    .map { $0.1 }

print(arrayString)

[["char": "Z"], ["char": "W"], ["char": "U"], ["name": "T"], ["name": "S"]]

[[“char”:“Z”],[“char”:“W”],[“char”:“U”],[“name”:“T”],[“name”:“S”] ]