代码之家  ›  专栏  ›  技术社区  ›  lf_araujo

向后浏览字典以查找上次更新值的时间

  •  0
  • lf_araujo  · 技术社区  · 6 年前

    我有一个字典数组,其中有两个感兴趣的键对。一个密钥对是日期,另一个是“investpercent”,它的值经常变化。我想知道最后一次约会是哪一次 investpercent 已更新。因此,请考虑:

    var importedRecords = [
    ["low": "1.0", "date": "2018-04-25", "objective": "2.0", "savings": "2.0", "high": "2.0", "expenses": "1.0", "investpercent": "3.0"], 
    ["low": "1.0", "date": "2018-05-25", "objective": "2.0", "savings": "2.0", "high": "2.0", "expenses": "1.0", "investpercent": "3.0"], 
    ["low": "1.0", "date": "2018-06-25", "objective": "2.0", "savings": "2.0", "high": "2.0", "expenses": "1.0", "investpercent": "4.0"], 
    ["low": "1.0", "date": "2018-07-25", "objective": "2.0", "savings": "2.0", "high": "2.0", "expenses": "1.0", "investpercent": "4.0"], 
    ["low": "1.0", "date": "2018-08-25", "objective": "2.0", "savings": "2.0", "high": "2.0", "expenses": "1.0", "investpercent": "5.0"], 
    ["low": "2", "date": "2018-09-26", "objective": "2.0", "savings": "2.0", "high": "4", "expenses": "1.0", "investpercent": "5.0"]
    ]
    

    现在,注意 投资百分比

    var lastentry = importedRecords.suffix(1)
    var lastdate: String = ""
    for record in importedRecords {
        if record["investpercent"] == lastentry["investpercent"] {
            lastdate = record["date"]
        }
    
    }
    

    • 投资百分比
    1 回复  |  直到 6 年前
        1
  •  1
  •   ielyamani    6 年前

    这应该可以做到,而且它使用 zip :

    let reversedRecords = importedRecords.reversed()
    let zipped = zip(reversedRecords.dropFirst(), reversedRecords)
    
    if let twoDictionaries = zipped.first(where: {$0["investpercent"] != $1["investpercent"]}),
        let date = twoDictionaries.1["date"] {
        print(date)
    }
    

    它打印出来了 2018-08-25