简体   繁体   English

如何基于另一个子数组过滤数组的数组

[英]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.

相关问题 iOS:如何根据参数值从另一个数组中过滤数组 - iOS: how to filter array from another array based on parameter value 基于另一个数组的过滤来过滤数组Swift - Filter Array based on the Filtering of Another Array Swift 使用谓词基于另一个数组过滤NSArray - Filter NSArray based on another array using predicate 使用基于谓词关键的另一个数组的谓词过滤数组 - Filter array using predicate based on another array that is key for predicate NSArray-如何根据与同一数组中另一个对象匹配的两个属性来过滤数组? - NSArray - How to filter an array based on two properties being matched to another object in the same array? 如何根据字典键过滤数组? - How to filter array based on dictionary key? 过滤字典数组。 Dictionary有另一个数组作为其键值对。 基于该数组中存在的键过滤数组 - Filter Array of dictionary. Dictionary has another array as its key value pair. Filter array based on key which is present in that array 过滤器数组,根据另一个数组的过滤器 - Filter array, according to the filter of another array 如何根据swift中的另一个数组过滤数组? - How can I filter array according to another array in swift? 如何在 Swift 中使用另一个数组中的元素过滤数组? - How To Filter Array Using Element in Another Array in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM