Swift数组中满足条件的值

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

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".

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

import UIKit

class ViewController: UIViewController {
    
    
    var sorted_polozky = Array<(key: String, value: Array<polozkyUcet>)>()

    struct polozkyUcet: Codable {
        let _id: Int
        let _masterid: Int
        var _rcptid: Int
        var _mnozstvi: String
        var _delene_mnozstvi: String
        let _recept: String
        var _cena: Double
        let _tisk: Int
        var _chod: Int
        var _chodnazev: String
        var _host: String
        var _popis: String
        var _cenazak: Double
    }



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        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)])]

        sorted_polozky = test
        print(sorted_polozky)
    }
}
英文:

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:

import UIKit

class ViewController: UIViewController {
    
    
    var sorted_polozky = Array&lt;(key: String, value: Array&lt;polozkyUcet&gt;)&gt;()

    struct polozkyUcet: Codable {
        let _id: Int
        let _masterid: Int
        var _rcptid: Int
        var _mnozstvi: String
        var _delene_mnozstvi: String
        let _recept: String
        var _cena: Double
        let _tisk: Int
        var _chod: Int
        var _chodnazev: String
        var _host: String
        var _popis: String
        var _cenazak: Double
    }


   

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        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: &quot;ŠKVARKOV&#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)])]

        sorted_polozky = test
        print(sorted_polozky)   
    }
}

答案1

得分: 1

以下是您要翻译的内容:

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

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

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

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

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

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

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

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

答案2

得分: 0

以下是您要翻译的内容:

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

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

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

struct polozkyUcet: Codable {
    let _tisk: Int
    var _chodnazev: String
}

let someArray: [polozkyUcet] = [
    polozkyUcet(_tisk: 1, _chodnazev: "Blah"),
    polozkyUcet(_tisk: 10, _chodnazev: "Předkrm"),
    polozkyUcet(_tisk: 5, _chodnazev: "Dezert"),
    polozkyUcet(_tisk: 0, _chodnazev: "Dezert")
]

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:

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:

struct polozkyUcet: Codable {
        let _tisk: Int
        var _chodnazev: String
    }

    
    let someArray : [polozkyUcet] = [
        polozkyUcet(_tisk: 1, _chodnazev: &quot;Blah&quot;),
        polozkyUcet(_tisk: 10, _chodnazev: &quot;Předkrm&quot;),
        polozkyUcet(_tisk: 5, _chodnazev: &quot;Dezert&quot;),
        polozkyUcet(_tisk: 0, _chodnazev: &quot;Dezert&quot;)
    ]
    
    let selectedElements = someArray.filter  { $0._tisk &gt; 0 &amp;&amp; ($0._chodnazev == &quot;Předkrm&quot; || $0._chodnazev == &quot;Dezert&quot;) }

答案3

得分: 0

以下是翻译好的内容:

最后我使用了:

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

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

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:

确定