Store array custom array information

I have a doubt when it comes to store array information, this is my API

WalletService.shared.getSiteDetails { siteDetails, error in
                
            }

siteDetails is going to give me the decodeJson information,
one of multiple information that is retrieving is

public let fuelGrades: [MBFuelGrades]?

Which contains this

public struct MBFuelGrades: Codable {
    public let fuelType: String?
    public let price: Double?
    public let priceTier: String?
}

so when my API call response I get this array by printing
it could contain multiple elements not only one

po siteDetails?.fuelGrades
▿ Optional<Array<MBFuelGrades>>
  ▿ some : 1 element
    ▿ 0 : MBFuelGrades
      ▿ fuelType : Optional<String>
        - some : "REGULAR"
      ▿ price : Optional<Double>
        - some : 1.339
      ▿ priceTier : Optional<String>
        - some : "CREDIT"

what I trying to do is store the fuelType and price, in separate arrays

for example this:

var fuelGradesTypes: [String] = [""]
var fuelGradesPrice: [Double] = [0.0]

my question is how can I store this elements inside the arrays?

>Solution :

You need

guard let grades = siteDetails.fuelGrades else { return }

fuelGradesTypes = grades.compactMap { $0.fuelType }
fuelGradesPrice = grades.compactMap { $0.price }

Leave a Reply