大家好,我有一个字典数组,你能告诉我如何根据字典键过滤这些数据。
DXGI_FORMAT_R1_UNORM
我想要实现的目标,每当我在搜索栏中输入任何内容时,它都会检查mall_name键并返回数组中的匹配值。
谢谢和问候
答案 0 :(得分:12)
这将为您提供所需的输出
目标 - C
NSArray *filteredData = [yourArrayContainingDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];
<强>夫特强>
let filteredData = yourArrayContainingDictionary.filter{
let string = $0["mall_name"] as! String
return string.hasPrefix("searchText")
}
希望这可以帮助你:)
答案 1 :(得分:10)
试试这个。 (谓词就像SQL查询一样)
对象C
NSArray *filterArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];
<强>夫特强>
var filterArray: [Any] = sourceArray.filter { NSPredicate(format: "(mall_name contains[c] %@)", searchText).evaluate(with: $0) }
它将返回其名称包含搜索字符串的条目。
答案 2 :(得分:3)
使用此解决方案
NSArray *filtered = [yourArrayOfDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", <your search text from search bar>]];
答案 3 :(得分:1)
NSArray *filtered = [arrName filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name == %@)", requiredMailName]];
答案 4 :(得分:1)
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", wantedMallID]];
答案 5 :(得分:1)
以下是我使用谓词和经典版本的2个版本:
let dict = [
[
"mall_id": 1,
"mall_name": "Amrita Shopping Complex"
],
[
"mall_id": 2,
"mall_name": "Ashoka Market"
] ]
// Example using Predicate
let mallNamePredicate = NSPredicate(format: "mall_name contains %@", "Ashoka")
let filteredWithPredicate = (dict as NSArray).filtered(using: mallNamePredicate)
// Classical filter example
let filtered = dict.filter { pair in
guard let mallName = pair["mall_name"] as? String else { return false }
return mallName.hasPrefix("Ashoka")
}
答案 6 :(得分:1)
这样做的更好的方法就像是
let mallArray = [
[
"mall_id": "M0550",
"mall_name": "Amrita Shopping Complex"
],
[
"mall_id": "M0509",
"mall_name": "Ashoka Market"
]
]
func isMatching(_ searchText: String) -> [[String: Any]] {
let filteredArray = mallArray.filter {
return $0["mall_name"]!.contains(searchText)
}
return filteredArray
}
let malls = isMatching("Ashoka")
print(malls)
答案 7 :(得分:0)
这会为您提供一系列仅包含requiredMallID
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", requiredMallID]];
例如,
NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", "M0550"]];
给你
(
{
"mall_id" = M0550;
"mall_name" = "Amrita Shopping Complex";
}
)