从数组中的字典数组中获取数据

时间:2018-02-06 07:53:27

标签: ios swift nsarray nsdictionary

我创建了Array Dictionaries

let tempArray = [["id":"1","Name":"ABC"],["id":"2","Name":"qwe"],["id":"3","Name":"rty"],["id":"4","Name":"uio"]]

现在我必须创建一个Name数组。

我所做的是:

var nameArray = [String]()
for dataDict in tempArray {
    nameArray.append(dataDict["Name"]!)
}

但是还有其他有效的方法吗?

2 个答案:

答案 0 :(得分:2)

您可以使用async function doAjax() { return await $.ajax({ type: "GET", url: "posts_replied_to.txt" }); } let lines = await doAjax() lines = content.split('\n') console.log(lines) (而不是flatMap),因为map可以过滤掉flatMap值(dict不具备密钥值的情况"名称"),即nil数组将被定义为names而不是[String]

[String?]

答案 1 :(得分:2)

使用compactMap flatMapdeprecated

let tempArray = [["id":"1","Name":"ABC"],["id":"2","Name":"qwe"],["id":"3","Name":"rty"],["id":"4","Name":"uio"]]
let name = tempArray.compactMap({ $0["Name"]})
print(name)

enter image description here