如何过滤出数组的数组 [英] How to filter out a Array of Array's

查看:79
本文介绍了如何过滤出数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个包含多个值的数组,我想尝试过滤掉搜索栏的索引.用英语举例来说就是一个例子.给我一个名称为"Name2"的索引,并通过检查每个索引的第一个值中的所有字符串来做到这一点.

代码:

 //多个错误varceivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4",葡萄",烂"]]filteredData = data.filter({$ 0 == searchBar.text})filteredData = ReceivedList.filter({$ 0 receiveList [1] == searchBar.Text})//我不太确定如何使用它,或者它是否很有用让searchPredicate = NSPredicate(格式:橙色CONTAINS [C]%@",searchText)let array =(receivedList as NSArray).filtered(使用:searchPredicate) 

我在这里检查了这些页面.

filtering-array-of-dictionaries-in-swift

swift-filter-dictionary

filter-array-of-objects多种标准和类型

和其他人结对,没有运气

解决方案

如果我正确阅读了该问题,则需要查找第一个与搜索模式匹配的数组的索引.下面演示了如何做到这一点:

  var receiveList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4",葡萄",烂"]]var searchText ="Name2"让索引= receiveList.index {$ 0 [0] == searchText}打印(索引) 

以下内容将列表过滤为仅包含第一个元素包含搜索文本的用户:

 让匹配项= receiveList.filter {$ 0 [0] .contains(searchText)} 

如果您想要匹配的索引,则可以使用:

 让匹配项= receiveList.enumerated().filter {$ 0.1 [0] .contains(searchText)}.map {$ 0.0} 

Hello I have a array that has multiple values and I want to try and filter out the index's for my search bar. An Example in English terms would be like. Give me the index for the name "Name2" and do this by checking for all the strings in the first value for each index.

Code:

 // Multiple Errors 

 var receivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4","Grape","Rotten"]]


 filteredData = data.filter({$0 == searchBar.text})
 filteredData = receivedList.filter({$0 receivedList[1] == searchBar.Text})


 //Im not really sure how to use this or if it's even useful 

 let searchPredicate = NSPredicate(format: "Orange CONTAINS[C] %@", searchText)

 let array = (receivedList as NSArray).filtered(using: searchPredicate)

Iv'e checked these pages on here.

filtering-array-of-dictionaries-in-swift

swift-filter-dictionary

filter-array-of-objects-with-multiple-criteria-and-types-in-swift

And a-couple others, with no luck

解决方案

If I read the question correctly, you want to find the index of the array who's first element matches a search pattern. The following demonstrates how to do that:

var receivedList = [["Name1","Apple","Fresh"],["Name2","Orange","Rotten"],["Name3","Pear","Fresh"],["Name4","Grape","Rotten"]]
var searchText = "Name2"

let index = receivedList.index { $0[0] == searchText }

print(index)

The following will filter your list to only those whose first element contains the search text:

let matches = receivedList.filter { $0[0].contains(searchText) }

If you want the indexes that match, then you can use:

let matches = receivedList
    .enumerated()
    .filter { $0.1[0].contains(searchText) }
    .map { $0.0 }

这篇关于如何过滤出数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆