swift map array to dictionary

extension Array {
    public func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key:Element] {
        var dict = [Key:Element]()
        for element in self {
            dict[selectKey(element)] = element
        }
        return dict
    }
}

// Example

struct Person {
    var name: String
    var surname: String
    var identifier: String
}

let arr = [Person(name: "John", surname: "Doe", identifier: "JOD"),
           Person(name: "Jane", surname: "Doe", identifier: "JAD")]
let dict = arr.toDictionary { $0.identifier }

print(dict) // Result: ["JAD": Person(name: "Jane", surname: "Doe", identifier: "JAD"), "JOD": Person(name: "John", surname: "Doe", identifier: "JOD")]

0
0
Krish 100200 points

                                    let myDictionary = myArray.reduce([Int: String]()) { (dict, person) -&gt; [Int: String] in
    var dict = dict
    dict[person.position] = person.name
    return dict
}

//[2: &quot;b&quot;, 3: &quot;c&quot;, 1: &quot;a&quot;]

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
swift create a dictionary from an array swift convert dict to array swift array to dictionary by property map array to dictionary swift swift convert dictionary to array swift create dictionary from array of objects swift array to dictionary by value swift array of dictionary swift map to dictionary map dictionary to arrays swift convert json array to dictionary in swift 5 array to dictionary in swift 5 assing array data to dictionary swift convert dictionary values to array swift swift map to dict array from dictionary values swift swift map array return dictionary swift dict keys to array swift set array dictionary swift map array to dictionary swift array with dictionary swift dictionary values as array dictionary values to array swift how to convert array to dictionary in swift swift dictionary values to array swift map list to dictionary swift dictionary keys to array swift array to dictionary array to dictionary swift convert dictionary keys to array swift swiftui set array to dictionary convert dictionary to array swift into statement array dictionary swift swift object to dictionary swift, convert a dictionary to another dictionary convert dictionary to an object swift convert dictionary to an array in swift swift array map to dictionary swift create dictionary from array swift map one array to another swift map array of objects swift dictionary to array dictionary to array swift how to convert array of dictionary to array of dictionary in swift swift map to dictioary swift map array into dictionary array to dictionary convert ios swift swift dictionary from array convert array to dictionary swift toDictonary example swift 5 how to turn an array into a dictionary swift swift convert array to dictionary
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source