Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Reduce `[URLQueryItem]` into `[String: Any]`

Currently I've got this chunky reduce function...

blah: [String: Any] = queryItems.reduce([String: Any]()) {
    (params: [String: Any], queryItem: URLQueryItem) in

    var output = params

    output[queryItem.name] = queryItem.value

    return output
}

I'm sure there is a much simpler way of doing this but I can't get my head around how that would work.

Is there a "better" way to do this?

By "better" I mean cleaner, shorter, more elegant, etc...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It's possible to use reduce(into:_:) instead of reduce(_:_). This both save you the lines and the overhead of copying params for each iteration:

let blah: [String: Any] = (urlComponents.queryItems ?? []).reduce(into: [:]) {
    params, queryItem in 
    params[queryItem.name] = queryItem.value
}

This method is preferred over reduce(_:_:) for efficiency when the result is a copy-on-write type, for example an Array or a Dictionary.

over 4 years ago · Santiago Trujillo Report

0

You can create a dictionary from the name and value of each query item with

let items = urlComponents.queryItems ?? []
let dict = Dictionary(items.lazy.map { ($0.name, $0.value as Any) },
                      uniquingKeysWith: { $1 })

In the case of a duplicate name, the later value wins (this can be controlled with the uniquingKeysWith: parameters).

Or remove the as Any cast to get a dictionary of type [String: String?]:

let items = urlComponents.queryItems ?? []
let dict = Dictionary(items.lazy.map { ($0.name, $0.value ) },
                      uniquingKeysWith: { $1 })

Alternatively

let items = urlComponents.queryItems ?? []
let dict = Dictionary(items.lazy.map { ($0.name, [$0.value] ) },
                      uniquingKeysWith: +)

to build a dictionary of type [String : [String?]], holding all values for each name.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!