I have two Arrays String
:
var labelOne: [String] = ["one", "two", "one", "four", "five"]
var idOne: [String] = ["sdcsadcasdc", "sad213e123wd", "32dsqfdwf", "dsadcdasc", "32rfwec"]
Now in program, "one" = "sdcsadcasdc"
, "two" = "sad213e123wd"
, "one" = "32dsqfdwf"
, "four" = "dsadcdasc"
, "five = "32rfwec"
.
I have a filter for labelOne
and put it in another Array String
:
var result = "one"
var searchLabelOne = labelOne.filter { $0.lowercased().contains(result!.lowercased())}
I want to get filtered indexes from searchLabelOne
and use it for get indexes from idOne
. For this example, I would create Array String searchIdOne
for searchLabelOne
with "one" and "one" and he must be:
var searchIdOne = ["sdcsadcasdc", "32dsqfdwf"]
Please, help
CodePudding user response:
Well if you want to keep the data structures as the way they are, you can first get the indexes from firstArray.
Then iterate through secondArray to get elements at those indexes.
var searchIdOne = labelOne.enumerated().filter( {$0.element == result}).map({ $0.offset}).map({ idOne[$0]})
CodePudding user response:
Zip the arrays.
var labelOne: [String] = ["one", "two", "one", "four", "five"]
var idOne: [String] = ["sdcsadcasdc", "sad213e123wd", "32dsqfdwf", "dsadcdasc", "32rfwec"]
let result = zip(labelOne, idOne).filter {$0.0.contains("one")}.map {$0.1}
print(result) // ["sdcsadcasdc", "32dsqfdwf"]
(Adjust the filter
predicate as desired, that was only a demo.)