如何过滤掉数组的数组

时间:2017-10-31 01:30:13

标签: arrays swift

你好我有一个有多个值的数组,我想尝试过滤掉我搜索栏的索引。用英语表示的例子就好。给我名称“Name2”的索引,并通过检查每个索引的第一个值中的所有字符串来执行此操作。

代码:

 // 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)

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

filtering-array-of-dictionaries-in-swift

swift-filter-dictionary

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

和其他人一起,没有运气

2 个答案:

答案 0 :(得分:4)

如果我正确地阅读了这个问题,你想找到第一个元素匹配搜索模式的数组的索引。以下演示了如何执行此操作:

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)

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

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

如果您希望匹配的索引,则可以使用:

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

答案 1 :(得分:1)

SELECT
  cr.idConsumption,
  AVG(cr.watts),
  a.idApliance,
  p.idProto
FROM
  consumptionregistry cr
INNER JOIN
  apliance a
ON
  a.idApliance = cr.apliance_idApliance
INNER JOIN
  proto_has_apliance phs
ON
  phs.apliance_idApliance = a.idApliance
INNER JOIN
  proto p
ON
  phs.Proto_idProto = p.idProto AND p.user_idUser=1