快速删除数组括号和双引号

时间:2019-11-08 06:09:36

标签: swift

在我的情况下,我有一个字符串数组,我需要删除数组括号和双引号,如下所示,该如何实现?

我尝试了以下代码

let selectedTeamMemberID = membersData.filter{$0.isSelected}.map{$0.userid}
let intArray = selectedTeamMemberID.map { Int($0 ?? "")!}
print("\(intArray)")

预期产量

["1","2","3"] change to 1,2,3

5 个答案:

答案 0 :(得分:1)

尝试一下

您可以使用内置方法连接一个字符串数组

let selectedTeamMemberID = membersData.filter{$0.isSelected}.map{$0.userid}
let output = selectedTeamMemberID.joined(separator: ",")

答案 1 :(得分:1)

如果您不想包括没有userId的对象,则也可以对该属性进行过滤并强制将其拆开。这样可以避免结果中出现“空”元素

let selected = membersData.filter{$0.isSelected && $0.userid != nil}.map {$0.userid!}.joined(separator: ",")

但是也许更有效和更清洁的解决方案是使用compactMap映射并过滤出零值

let selected = membersData.filter{$0.isSelected}.compactMap {$0.userid}.joined(separator: ",")

答案 2 :(得分:0)

在打印数组时,它会打印字符串表示形式。

您需要遍历数组并打印内容

"scripts": {
    "start": "react-scripts start",
    "build": "webpack --mode production",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
},
"proxy": "http://url../v1",
"eslintConfig": {
    "extends": "react-app"
},

答案 3 :(得分:0)

我们可以在数据映射后添加Joined

let selectedID = membersData.filter{$0.isSelected}.map{($0.userid ?? "")}.joined(separator: ",") // ["1","2","3"] to 1,2,3

答案 4 :(得分:0)

您可以这样做:

selectedTeamMemberID.joined(separator: ",")

:)