0

I need to convert an array of key and values, such like:

["key1", "value1", "key2", "value2"]

into a dictionary:

{"key1": "value1", "key2": "value2"}

I can go through the array by a step of two, and get n and n+1 for key and value, respectively, but is there any smart way of doing this?

Thanks!

1
  • 2
    You can be more functional, and use stride, map, filter etc, but you will still be doing the same "go through the array by a step of two, and get n and n+1 for key and value". I don't think you can avoid writing the number "2" and getting n and n + 1, without sacrificing a lot of readability. Commented Feb 10, 2021 at 2:29

1 Answer 1

2

You can get your collection in subsequences of two and reduce them into a dictionary:

extension Collection {
    func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
        sequence(state: startIndex) { start in
            guard start < self.endIndex else { return nil }
            let end = self.index(start, offsetBy: maxLength, limitedBy: self.endIndex) ?? self.endIndex
            defer { start = end }
            return self[start..<end]
        }
    }
}

let kvs = ["key1", "value1", "key2", "value2"]

let dictionary = kvs
    .unfoldSubSequences(limitedTo: 2)
    .reduce(into: [:]) {
        guard $1.count == 2, let key = $1.first, let value = $1.last else { return }
        $0[key] = value
    }
dictionary  // ["key1": "value1", "key2": "value2"]

Another approach using stride. This assumes your collection has an even number of elements:

let kvs = ["key1", "value1", "key2", "value2"]

let dictionary = stride(from: 0, to: kvs.count, by: 2)
    .reduce(into: [:]) { $0[kvs[$1]] = kvs[$1+1] }
dictionary  // ["key1": "value1", "key2": "value2"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.