从对象数组中删除对象swift [复制]

时间:2022-09-25 07:53:48

This question already has an answer here:

这个问题在这里已有答案:

I have an issue and need your help. I have array of objects

我有一个问题,需要你的帮助。我有一些对象

var arrayDirection: [Directions]

every object has properties:

每个对象都有属性:

var name: String?
var uniqueId: Int?

I need find and leave only one object whose values of properties duplicate values of properties of another objects from that array.

我需要查找并保留一个对象,其属性值与该数组中的另一个对象的属性值重复。

For example I print in console:

例如我在控制台中打印:

for object in arrayDirection {
    print(object.name, object.uniqueId)
}

and see:

并看到:

Optional("name1") Optional(833)

可选(“name1”)可选(833)

Optional("name1") Optional(833)

可选(“name1”)可选(833)

Optional("name2") Optional(833)

可选(“name2”)可选(833)

Optional("name4") Optional(833)

可选(“name4”)可选(833)

Optional("name1") Optional(833)

可选(“name1”)可选(833)

Optional("name1") Optional(862)

可选(“name1”)可选(862)

So, I need remove Optional("name1") Optional(833) because there are 3 in array and leave only one as result I'd like to see:

所以,我需要删除Optional(“name1”)可选(833)因为数组中有3个并且只留下一个我希望看到的结果:

Optional("name1") Optional(833)

可选(“name1”)可选(833)

Optional("name2") Optional(833)

可选(“name2”)可选(833)

Optional("name3") Optional(833)

可选(“name3”)可选(833)

Optional("name1") Optional(862)

可选(“name1”)可选(862)

2 个解决方案

#1


1  

Actually you need to remove duplicate from your data set. This link will help what you want to achieve.

实际上,您需要从数据集中删除重复项。此链接将有助于您实现的目标。

However in short, use Set to avoid duplicate data.

但简而言之,请使用Set以避免重复数据。

#2


0  

You could reduce the array:

你可以减少数组:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0.uniqueId == direction.uniqueId }) ? result : result + [direction]
}

Obviously this piece of code assumes that you only want to check if the IDs duplicate, if you want to check whether whole objects duplicate you could either implement the Equatable protocol for your Directions type, or do it like that:

显然,这段代码假设您只想检查ID是否重复,如果要检查整个对象是否重复,您可以为Directions类型实现Equatable协议,或者这样做:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0.uniqueId == direction.uniqueId && $0.name == direction.name }) ? result : result + [direction]
}

And with Equatable protocol implemented it gets actually really simple:

通过实现Equatable协议,它实际上非常简单:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0 == direction }) ? result : result + [direction]
}

#1


1  

Actually you need to remove duplicate from your data set. This link will help what you want to achieve.

实际上,您需要从数据集中删除重复项。此链接将有助于您实现的目标。

However in short, use Set to avoid duplicate data.

但简而言之,请使用Set以避免重复数据。

#2


0  

You could reduce the array:

你可以减少数组:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0.uniqueId == direction.uniqueId }) ? result : result + [direction]
}

Obviously this piece of code assumes that you only want to check if the IDs duplicate, if you want to check whether whole objects duplicate you could either implement the Equatable protocol for your Directions type, or do it like that:

显然,这段代码假设您只想检查ID是否重复,如果要检查整个对象是否重复,您可以为Directions类型实现Equatable协议,或者这样做:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0.uniqueId == direction.uniqueId && $0.name == direction.name }) ? result : result + [direction]
}

And with Equatable protocol implemented it gets actually really simple:

通过实现Equatable协议,它实际上非常简单:

let reducedArray: [Direction] = myArray.reduce([]) { result, direction in
   result.contains(where: { $0 == direction }) ? result : result + [direction]
}