我试图通过某个元素过滤我的数组。我想记录类型包含"收入"和createdAt等于日期但$0.createdAt!
是日期类型,因此我无法使用.contain("")
,我可以使用什么?
recordFilter = record.filter { $0.recordtype!.contains("Income") /*|| $0.createdAt! = recordItem.createdAt!*/}
recordFilter输出:(此结果尚未按createdAt过滤)
[<Record: 0x600000099fa0> (entity: Record; id: 0xd0000000003c0000 <x-coredata://913F25A5-B2C9-4646-9091-5EFE7F906908/Record/p15> ; data: {
accountbook = "first book";
amount = "\U00a59.99";
assest = nil;
category = "\U6295\U8d44";
createdAt = "2017-11-16 16:00:00 +0000";
date = nil;
id = 15;
recordtype = "\U6536\U5165";
remark = "";
toAccBook = nil;
}), <Record: 0x600000099ff0> (entity: Record; id: 0xd000000000400000 <x-coredata://913F25A5-B2C9-4646-9091-5EFE7F906908/Record/p16> ; data: {
accountbook = "first book";
amount = "\U00a56.58";
assest = nil;
category = "\U5de5\U8d44";
createdAt = "2017-11-16 16:00:00 +0000";
date = nil;
id = 16;
recordtype = "\U6536\U5165";
remark = "";
toAccBook = nil;
})]
之后,我试图获取数组中的所有数量字符串并将其求和。但是当我使用这段代码时,我发现它是所有单独的字符串,我如何将它组合在数组中并加以总结或者有其他方法来实现它?
let sum = recordFilter.map({Int($0.amount!.dropFirst())})
//[nil, nil]
for i in 0..<recordFilter.count{
if recordFilter[i].amount != nil{
let amountList = recordFilter[i].amount!.dropFirst()
print(amountList) // I found that it's separate string.
//9.99
//11.22
// ...
//let intArray = amountList.map { Int($0)!}
//let sum = amountList.reduce(0, +)
//print(sum)
}
}
答案 0 :(得分:1)
好吧,如果你想完成两个案例(public function search()
{
$search = request('search');
$searchType = request('searchType');
if (strcmp($searchType, "posts") == 0) {
$posts = Post::approved()->where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->get();
} elseif (strcmp($searchType, "products") == 0) {
$products = Product::approved()->where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->get();
}
return view('frontend.search', compact('posts', 'products'));
}
== recordtype
&amp;&amp; Income
== createdAt
)那么为什么你没有过滤使用Your Date
?
&&
如果我遗失了什么,请试试让我知道吗?
如果你想在这里总结一下,你去:
recordFilter = record.filter { return $0.recordtype!.contains("Income") && $0.createdAt! == recordItem.createdAt!}