通过对象属性在数组中获得不同的元素。

时间:2022-02-18 13:37:08

I have an array of object. I want to get distinct elements in this array by comparing objects based on its name property

我有一个对象数组。通过比较对象的名称属性,我想在这个数组中获得不同的元素

class Item {
var name: String
init(name: String) {
    self.name = name
}
}
let items = [Item(name:"1"), Item(name:"2"), Item(name:"1"), Item(name:"1"),Item(name:"3"), Item(name:"4")]

result:

结果:

let items = [Item(name:"1"), Item(name:"2"),Item(name:"3"), Item(name:"4")]

how can I do this in swift?

我怎么能在斯威夫特身上做这件事呢?

3 个解决方案

#1


1  

Hope this will help you:

希望这对你有所帮助:

class Item:Equatable, Hashable {
    var name: String
    init(name: String) {
        self.name = name
    }
    var hashValue: Int{
      return name.hashValue
    }

}

func ==(lhs: Item, rhs: Item) -> Bool {
    return lhs.name == rhs.name
}


let items = [Item(name:"1"), Item(name:"2"), Item(name:"1"), Item(name:"1"),Item(name:"3"), Item(name:"4")]

var uniqueArray = Array(Set(items))

#2


4  

Here is an Array extension to return the unique list of objects based on a given key:

下面是一个数组扩展,根据给定的键返回唯一的对象列表:

extension Array {
    func unique<T:Hashable>(by: ((Element) -> (T)))  -> [Element] {
        var set = Set<T>() //the unique list kept in a Set for fast retrieval
        var arrayOrdered = [Element]() //keeping the unique list of elements but ordered
        for value in self {
            if !set.contains(by(value)) {
                set.insert(by(value))
                arrayOrdered.append(value)
            }
        }

        return arrayOrdered
    }
}

For your example you can do:

举个例子,你可以这样做:

let uniqueBasedOnName = items.unique{$0.name}

#3


0  

In Swift you can use Equatable protocol to distinguish unique element in an Array of object.

在Swift中,您可以使用Equatable协议来区分一个对象数组中的唯一元素。

 struct Item:Equatable{
        var name:String
        var price:Double

        init(name:String,price:Double) {
            self.name = name
            self.price = price
        }

        static func ==(lhs: Item, rhs: Item) -> Bool{
            return lhs.name == rhs.name
        }
    }

    class ViewController: UIViewController {

       var books = [Item]()
        override func viewDidLoad() {
            super.viewDidLoad()
            items.append(Item(name: "Example 1", price: 250.0))
            items.append(Item(name: "Example 2", price: 150.0))
            items.append(Item(name: "Example 1", price: 150.0))
            items.append(Item(name: "Example 1", price: 150.0))
            items.append(Item(name: "Example 3", price: 100.0))
            items.unique().forEach { (item) in
                print(item.name)
            }
        }

    }

    extension Sequence where Iterator.Element: Equatable {
        func unique() -> [Iterator.Element] {
            return reduce([], { collection, element in collection.contains(element) ? collection : collection + [element] })
        }
    }

#1


1  

Hope this will help you:

希望这对你有所帮助:

class Item:Equatable, Hashable {
    var name: String
    init(name: String) {
        self.name = name
    }
    var hashValue: Int{
      return name.hashValue
    }

}

func ==(lhs: Item, rhs: Item) -> Bool {
    return lhs.name == rhs.name
}


let items = [Item(name:"1"), Item(name:"2"), Item(name:"1"), Item(name:"1"),Item(name:"3"), Item(name:"4")]

var uniqueArray = Array(Set(items))

#2


4  

Here is an Array extension to return the unique list of objects based on a given key:

下面是一个数组扩展,根据给定的键返回唯一的对象列表:

extension Array {
    func unique<T:Hashable>(by: ((Element) -> (T)))  -> [Element] {
        var set = Set<T>() //the unique list kept in a Set for fast retrieval
        var arrayOrdered = [Element]() //keeping the unique list of elements but ordered
        for value in self {
            if !set.contains(by(value)) {
                set.insert(by(value))
                arrayOrdered.append(value)
            }
        }

        return arrayOrdered
    }
}

For your example you can do:

举个例子,你可以这样做:

let uniqueBasedOnName = items.unique{$0.name}

#3


0  

In Swift you can use Equatable protocol to distinguish unique element in an Array of object.

在Swift中,您可以使用Equatable协议来区分一个对象数组中的唯一元素。

 struct Item:Equatable{
        var name:String
        var price:Double

        init(name:String,price:Double) {
            self.name = name
            self.price = price
        }

        static func ==(lhs: Item, rhs: Item) -> Bool{
            return lhs.name == rhs.name
        }
    }

    class ViewController: UIViewController {

       var books = [Item]()
        override func viewDidLoad() {
            super.viewDidLoad()
            items.append(Item(name: "Example 1", price: 250.0))
            items.append(Item(name: "Example 2", price: 150.0))
            items.append(Item(name: "Example 1", price: 150.0))
            items.append(Item(name: "Example 1", price: 150.0))
            items.append(Item(name: "Example 3", price: 100.0))
            items.unique().forEach { (item) in
                print(item.name)
            }
        }

    }

    extension Sequence where Iterator.Element: Equatable {
        func unique() -> [Iterator.Element] {
            return reduce([], { collection, element in collection.contains(element) ? collection : collection + [element] })
        }
    }