I am trying to filter an array of array, based on another subarray. I am giving example below to make the requirement more clear. Please note that order of filterArray is important. I can iterate myArrayofArray using for loop, and then compare the element of each iterated element with filterArray
. But I think Filter
can be used in this case to get the resultArray. But a bit confused, how I'll implement it.
myArrayofArray = [[1,0,2], [1,2,0], [1,3,4], [1,2,1]]
filterArray = [1,0]
resultArray = [1,0,2]
The easiest way would be
let result = myArrayofArray.filter { $0.starts(with: filterArray) }
This will return a [[Int]]
with zero or more matching arrays.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.