Swift数组中满足条件的值

huangapple go评论154阅读模式
英文:

Swift Array where the values meet the conditions

问题

I have array and I would like to get only the records that have the field _tisk > 0 and _chodnazev = "Předkrm" or "Dezert".

如何做到这一点?以下是代码:

  1. import UIKit
  2. class ViewController: UIViewController {
  3. var sorted_polozky = Array<(key: String, value: Array<polozkyUcet>)>()
  4. struct polozkyUcet: Codable {
  5. let _id: Int
  6. let _masterid: Int
  7. var _rcptid: Int
  8. var _mnozstvi: String
  9. var _delene_mnozstvi: String
  10. let _recept: String
  11. var _cena: Double
  12. let _tisk: Int
  13. var _chod: Int
  14. var _chodnazev: String
  15. var _host: String
  16. var _popis: String
  17. var _cenazak: Double
  18. }
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do any additional setup after loading the view.
  22. let test = [(key: "Předkrm", value: [polozkyUcet(_id: 456037, _masterid: 145138, _rcptid: 19276, _mnozstvi: "1", _delene_mnozstvi: "0", _recept: "ŠKVARKOVÁ POMAZÁNKA", _cena: 85.0, _tisk: -1, _chod: 1, _chodnazev: "Předkrm", _host: "0", _popis: "", _cenazak: 85.0)]), (key: "Hlavní chod", value: [polozkyUcet(_id: 456038, _masterid: 145138, _rcptid: 17828, _mnozstvi: "1", _delene_mnozstvi: "0", _recept: "PÁRKY OD KREJCARŮ,BRAMBOROVÁ KAŠE", _cena: 135.0, _tisk: -10, _chod: 10, _chodnazev: "Hlavní chod", _host: "0", _popis: "", _cenazak: 135.0)]), (key: "Dezert", value: [polozkyUcet(_id: 456039, _masterid: 145138, _rcptid: 3047, _mnozstvi: "0,5", _delene_mnozstvi: "0", _recept: "RAKVIČKA SE ŠLEHAČKOU", _cena: 20.0, _tisk: -30, _chod: 30, _chodnazev: "Dezert", _host: "0", _popis: "", _cenazak: 40.0), polozkyUcet(_id: 456112, _masterid: 145138, _rcptid: 1117, _mnozstvi: "1", _delene_mnozstvi: "0", _recept: "KAPR FILET NA KMÍNĚ", _cena: 168.0, _tisk: 30, _chod: 30, _chodnazev: "Dezert", _host: "0", _popis: "", _cenazak: 168.0)])]
  23. sorted_polozky = test
  24. print(sorted_polozky)
  25. }
  26. }
英文:

I have array and I would like to get only the records that have the field _tisk > 0 and _chodnazev = "Předkrm" or "Dezert".

How to do that? Here is code:

  1. import UIKit
  2. class ViewController: UIViewController {
  3. var sorted_polozky = Array&lt;(key: String, value: Array&lt;polozkyUcet&gt;)&gt;()
  4. struct polozkyUcet: Codable {
  5. let _id: Int
  6. let _masterid: Int
  7. var _rcptid: Int
  8. var _mnozstvi: String
  9. var _delene_mnozstvi: String
  10. let _recept: String
  11. var _cena: Double
  12. let _tisk: Int
  13. var _chod: Int
  14. var _chodnazev: String
  15. var _host: String
  16. var _popis: String
  17. var _cenazak: Double
  18. }
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. // Do any additional setup after loading the view.
  22. let test = [(key: &quot;Předkrm&quot;, value: [polozkyUcet(_id: 456037, _masterid: 145138, _rcptid: 19276, _mnozstvi: &quot;1&quot;, _delene_mnozstvi: &quot;0&quot;, _recept: &quotKVARKOV&#193; POMAZ&#193;NKA&quot;, _cena: 85.0, _tisk: -1, _chod: 1, _chodnazev: &quot;Předkrm&quot;, _host: &quot;0&quot;, _popis: &quot;&quot;, _cenazak: 85.0)]), (key: &quot;Hlavn&#237; chod&quot;, value: [polozkyUcet(_id: 456038, _masterid: 145138, _rcptid: 17828, _mnozstvi: &quot;1&quot;, _delene_mnozstvi: &quot;0&quot;, _recept: &quot;P&#193;RKY OD KREJCARŮ,BRAMBOROV&#193; KAŠE&quot;, _cena: 135.0, _tisk: -10, _chod: 10, _chodnazev: &quot;Hlavn&#237; chod&quot;, _host: &quot;0&quot;, _popis: &quot;&quot;, _cenazak: 135.0)]), (key: &quot;Dezert&quot;, value: [polozkyUcet(_id: 456039, _masterid: 145138, _rcptid: 3047, _mnozstvi: &quot;0,5&quot;, _delene_mnozstvi: &quot;0&quot;, _recept: &quot;RAKVIČKA SE ŠLEHAČKOU&quot;, _cena: 20.0, _tisk: -30, _chod: 30, _chodnazev: &quot;Dezert&quot;, _host: &quot;0&quot;, _popis: &quot;&quot;, _cenazak: 40.0), polozkyUcet(_id: 456112, _masterid: 145138, _rcptid: 1117, _mnozstvi: &quot;1&quot;, _delene_mnozstvi: &quot;0&quot;, _recept: &quot;KAPR FILET NA KM&#205;NĚ&quot;, _cena: 168.0, _tisk: 30, _chod: 30, _chodnazev: &quot;Dezert&quot;, _host: &quot;0&quot;, _popis: &quot;&quot;, _cenazak: 168.0)])]
  23. sorted_polozky = test
  24. print(sorted_polozky)
  25. }
  26. }

答案1

得分: 1

以下是您要翻译的内容:

要筛选出至少有一个元素与筛选条件匹配的元组,您可以执行以下操作:

  1. let result = test.filter {
  2. $0.value.contains { item in item._tisk > 0 && ["Předkrm", "Dezert"].contains(item._chodnazev) }
  3. }

要筛选出并返回仅符合条件的元素,我们需要将我们的筛选操作包装在 reduce(into:) 中,以便每个元组只进行一次筛选:

  1. let result = test.reduce(into: [(key: String, value: [polozkyUcet])]()) {
  2. let value = $1.value.filter { item in item._tisk > 0 && ["Předkrm", "Dezert"].contains(item._chodnazev) }
  3. if !value.isEmpty {
  4. $0.append((key: $1.key, value: value))
  5. }
  6. }
英文:

To filter out any tuple that has at least one element that matches the filter you can do

  1. let result = test.filter {
  2. $0.value.contains { item in item._tisk &gt; 0 &amp;&amp; [&quot;Předkrm&quot;, &quot;Dezert&quot;].contains(item._chodnazev) }
  3. }

To filter out and return only the elements that match condition we need to wrap our filter in reduce(into:) so that the filtering is done once per tuple

  1. let result = test.reduce(into: [(key: String, value: [polozkyUcet])]()) {
  2. let value = $1.value.filter{ item in item._tisk &gt; 0 &amp;&amp; [&quot;Předkrm&quot;, &quot;Dezert&quot;].contains(item._chodnazev) }
  3. if value.isEmpty == false {
  4. $0.append((key: $1.key, value: value))
  5. }
  6. }

答案2

得分: 0

以下是您要翻译的内容:

当您有一个名为 "polozkhUcet" 的数组时,以下代码可以实现您的需求:

  1. someArray.filter { $0._tisk > 0 && ($0._chodnazev == "Předkrm" || $0._chodnazev == "Dezert") }

我无法运行您的代码,但以下代码对我有效:

  1. struct polozkyUcet: Codable {
  2. let _tisk: Int
  3. var _chodnazev: String
  4. }
  5. let someArray: [polozkyUcet] = [
  6. polozkyUcet(_tisk: 1, _chodnazev: "Blah"),
  7. polozkyUcet(_tisk: 10, _chodnazev: "Předkrm"),
  8. polozkyUcet(_tisk: 5, _chodnazev: "Dezert"),
  9. polozkyUcet(_tisk: 0, _chodnazev: "Dezert")
  10. ]
  11. let selectedElements = someArray.filter { $0._tisk > 0 && ($0._chodnazev == "Předkrm" || $0._chodnazev == "Dezert") }
英文:

When you have an array of polozkhUcet then this should do the trick:

  1. someArray.filter { $0._tisk &gt; 0 &amp;&amp; ($0._chodnazev == &quot;Předkrm&quot; || $0._chodnazev == &quot;Dezert&quot;) }

I could not get your code to run but this works for me:

  1. struct polozkyUcet: Codable {
  2. let _tisk: Int
  3. var _chodnazev: String
  4. }
  5. let someArray : [polozkyUcet] = [
  6. polozkyUcet(_tisk: 1, _chodnazev: &quot;Blah&quot;),
  7. polozkyUcet(_tisk: 10, _chodnazev: &quot;Předkrm&quot;),
  8. polozkyUcet(_tisk: 5, _chodnazev: &quot;Dezert&quot;),
  9. polozkyUcet(_tisk: 0, _chodnazev: &quot;Dezert&quot;)
  10. ]
  11. let selectedElements = someArray.filter { $0._tisk &gt; 0 &amp;&amp; ($0._chodnazev == &quot;Předkrm&quot; || $0._chodnazev == &quot;Dezert&quot;) }

答案3

得分: 0

以下是翻译好的内容:

最后我使用了:

  1. let result = sorted_polozky.map{(key, value) in return value.filter{$0._tisk > 0}}
  2. let filter = result.filter{!$0.isEmpty}
  3. print(filter)
英文:

Finally I used:

let result = sorted_polozky.map{(key, value) in return value.filter{$0._tisk > 0}}

let filter = result.filter{!$0.isEmpty}

print(filter)

huangapple
  • 本文由 发表于 2023年5月7日 00:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定