Maxim Poltoratsky
Maxim Poltoratsky

Reputation: 65

Append Arraay in to Array of Arrays if it has same values inside . swift

From server response i'm getting this 5 array of arrays (Some of them is array of arrays)

What i want is replace matching value from array "Legs" () with full array that have matching value. For example if value Legs["Id"] matching with value Itineraries ["OutboundLegId"] i want to replace full array "Itineraries" with this value

1st:

Itineraries =     (
            {
        BookingDetailsLink =             {
            Body = "";
            Method = PUT;
            Uri = "";
        };
        InboundLegId = "10081-1701231145--31722-1-16216-1701231854";
        OutboundLegId = "16216-1701222315--31722-1-10081-1701230921";
        PricingOptions =             (
                            {
                Agents =                     (
                    4132306
                );
                DeeplinkUrl = "http://partners.api";
                Price = 706;
                QuoteAgeInMinutes = 121;
            }
        );
    },

2nd:

Legs =     (
            {
        Arrival = "2017-01-22T20:33:00";
        Carriers =             (
            870
        );
        Departure = "2017-01-22T08:59:00";
        DestinationStation = 10081;
        Directionality = Outbound;
        Duration = 514;
        FlightNumbers =             (
                            {
                CarrierId = 870;
                FlightNumber = 2288;
            },
                            {
                CarrierId = 870;
                FlightNumber = 178;
            }
        );
        Id = "16216-1701220859--32171-1-10081-1701222033";
        JourneyMode = Flight;
        OperatingCarriers =             (
            870
        );
        OriginStation = 16216;
        SegmentIds =             (
            0,
            1
        );
        Stops =             (
            13411
        );
    },

that code shows how convert data in to the objects:

      let agents: Array = json["Agents"].arrayValue
      let carriers: Array = json["Carriers"].arrayValue
      let places: Array = json["Places"].arrayValue
      let legs: Array = json["Legs"].arrayValue
      let ss: Array = json["Itineraries"].arrayValue
      let itineraries: Array = json["Itineraries"].arrayValue.flatMap({$0["PricingOptions"].arrayValue})

That what i want but not fully understand how to prevent to swift code:

for legID in legs {
if legs.id.match with itineraries.id {
        legs.id.apped(itineraries)
}

}

Upvotes: 0

Views: 145

Answers (1)

Frankie
Frankie

Reputation: 11928

Here's an example of how you might begin to convert your data to structs

struct Itinerary {

    struct PricingOptions {
        var deepLinkURL: String?
        ...

        init(dictionary: [String : Any]?) {
            self.deepLinkURL = dictionary?["DeepLinkURL"] as? String
            ...
        }
    }

    var inboundLegID: String?
    var outboundLegID: String?
    var pricingOptions: [PricingOptions]?
    ...

    init(dictionary: [String : Any]) {

        self.inboundLegID = dictionary["InboundLegID"] as? String
        self.outboundLegID = dictionary["OutboundLegID"] as? String

        if let options = dictionary["PricingOptions"] as? [[String : Any]] {
            self.pricingOptions = options.map({ PricingOptions(dictionary: $0) })
        }

        ...
    }

    func contains(leg: Leg) -> Bool {
        return leg.id == inboundLegID || leg.id == outboundLegID
    }
}

struct Leg {
//  apply same idea here
    ...
}

Possible usage:

let allItineraries = itinerariesJSON.map({ Itinerary($0) })

let someItinerary = allItineraries.find({ $0.contains(someLeg) })

let containsLeg = someItinerary.contains(someLeg)

Upvotes: 1

Related Questions