Find Object with Property in Array

前端 未结 5 950
日久生厌
日久生厌 2020-11-29 02:31

is there a possibility to get an object from an array with an specific property? Or do i need to loop trough all objects in my array and check if an property is the specific

5条回答
  •  时光说笑
    2020-11-29 03:06

    Here is other way to fetch particular object by using object property to search an object in array.

    if arrayTicketsListing.contains({ $0.status_id == "2" }) {
          let ticketStatusObj: TicketsStatusList = arrayTicketsListing[arrayTicketsListing.indexOf({ $0.status_id == "2" })!]
          print(ticketStatusObj.status_name)
    }  
    

    Whereas, my arrayTicketsListing is [TicketsStatusList] contains objects of TicketsStatusList class.

    // TicketsStatusList class
    
    class TicketsStatusList {
        internal var status_id: String
        internal var status_name: String
        init(){
            status_id = ""
            status_name = ""
        }
    }
    

提交回复
热议问题