[英]How to filter array of array based on another subarray
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.
请注意,filterArray的顺序很重要。 I can
iterate myArrayofArray using for loop, and then compare the element of each iterated element with filterArray
. 我可以
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. 但是我认为可以在这种情况下使用
Filter
来获取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. 这将返回具有零个或多个匹配数组的
[[Int]]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.