如何通过多个元素过滤数组并在Swift中找到总和?(How to filter the array by multiple element and find the sum in Swift?)
我试图通过一些元素来过滤我的数组。 我想记录类型包含“收入”和
$0.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) } }
I'm trying to filter my array by some element. I want recordtype is contain "Income" and createdAt equal to a date but
$0.createdAt!
is an date type so I can't use.contain("")
, what can I use?recordFilter = record.filter { $0.recordtype!.contains("Income") /*|| $0.createdAt! = recordItem.createdAt!*/}
recordFilter output: (this result is not filter by createdAt yet)
[<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; })]
After that, I trying to get all the amount string in array and sum it. But I found that it's all separate string when I use this code, how can I group it in array and sum it or have any other way to do it?
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) } }
原文:https://stackoverflow.com/questions/47343382
满意答案
原因是正则表达式中有
<
符号,并且在浏览器中它们被解释为标记。 要避免此类行为,您可以通过htmlspecialchars
转义此类字符:echo htmlspecialchars('/' . $this->rules['usuario']['regexp'] . '/');
The reason is that there are
<
symbols in the regexp, and in the browser they are interpreted as tags. To avoid such behavior, you can escape such characters viahtmlspecialchars
:echo htmlspecialchars('/' . $this->rules['usuario']['regexp'] . '/');
相关问答
更多使用PHP在数组中存储数组(Storing array within an array using PHP)
PHP RegEx拆分为2D数组?(PHP RegEx split to 2D array?)
使用REGEX查找PHP(Find PHP with REGEX)
在数组上执行正则表达式并存储在另一个数组中(doing a regex on an array and storing in another array)
在PHP Regex中需要帮助(Need Help in PHP Regex)
Php:正则表达式 - 如何提取匹配的多个部分并将其存储在数组中?(Php:Regex-How to extract multiple portions of match and store it in an array?)
PHP / MySQL - 将数组存储在数据库中(PHP/MySQL - Storing array in database)
Regex PHP中的'['和']'('[' and ']' in Regex PHP)
在数组PHP中存储REGEX(Storing REGEX in array PHP)
PHP中的RegEx返回数组(RegEx in PHP to return array)
相关文章
更多千锋首发Swift视频教程
Swift入门视频教程-尚学堂视频教程
lucene4.7 过滤Filter(六)
The content of element type "package" must match "...
could not find system property or JNDI
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的解决方案
Solr4:利用Filter实现两组关键词组合查询
Filter 简单应用实例求详解
线性渐变-linear-gradient和滤镜opacity/filter的透明效果兼容性解决方案及其RGB/RGBA与16进制转换方法
移动MM failed to find resource file{mmiap.xml}
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!