1

I need to create a dictionary from array with custom type for first index of the array.

Sample array : ["ABC","ZYZ","123"]

Required result : [{"name" : "ABC", "type:"A"},{"name" : "ZYZ", "type:"B"},{"name" : "123", "type:"B"}]

Note type A for first index.

My code

for url in urlArray {
        urlDict["name"] = url
    }
3
  • 3
    What's the logic here? Why is the first dictionary with type:A but the others with type:B? Commented Sep 10, 2019 at 6:41
  • First index of the array needs to sent to sever separately. Commented Sep 10, 2019 at 6:45
  • So it's always the first index? It doesn't matter what the contents of the array are, right? Commented Sep 10, 2019 at 6:46

5 Answers 5

2

You can do a map, and then individually change the type of the first dictionary:

var dicts = urlArray.map { ["name": $0, "type": "B"] }
dicts[0]["type"] = "A"

Seeing how all your dictionary keys are all the same, and that you are sending this to a server, a Codable struct might be a better choice.

struct NameThisProperly : Codable {
    var name: String
    var type: String
}

var result = urlArray.map { NameThisProperly(name: $0, type: "B") }
result[0].type = "A"
do {
    let data = try JSONDecoder().encode(result)
    // you can now send this data to server
} catch let error {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

I suppose you can use a high order function such as map or reduce

Here is an example using reduce

var array = ["ABC","ZYZ","123"]

var result = array.reduce([[String: String]](), { (previous, current) -> [[String: String]] in
    let type = previous.count == 0 ? "A" : "B"
    let dictForCurrent = [
        "name": current,
        "type": type
    ]
    return previous + [dictForCurrent]
})

print(result)

The result:

[["type": "A", "name": "ABC"], ["type": "B", "name": "ZYZ"], ["name": "123", "type": "B"]]

Comments

2

Use reduce to convert array to dictionary:

let resultDict: [String: String]
      = array.reduce(into: [:]) { dict, url in
        dict["name"] = url
      }

The result will look like:

[
  "name": URL1,
  "name": URL2
]

1 Comment

Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.
1

Use map(_:) to convert each element of the array to dictionary like so,

let arr = ["ABC","ZYZ","123"]
let result = arr.map { (element) -> [String:String] in
    var dict = [String:String]()
    dict["name"] = element
    if let char = element.first {
        dict["type"] = String(char)
    }
    return dict
}

print(result)

Comments

1

since you are concern about the index, my approach will be using enumerated() which gives out the index


   let array = ["ABC","ZYZ","123"]
   var results: [[String: String]] = []
   for (i, content) in array.enumerated() {
      let type: String = i == 0 ? "A" : "B"
      results.append(["name": content, "type": type])
    }
    print(result)

// [["type": "A", "name": "ABC"], ["name": "ZYZ", "type": "B"], ["type": "B", "name": "123"]]

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.