javascript - 如何从 map 中的小部件搜索和访问数据?

标签 javascript dictionary flutter dart

这是我的示例 map :

Map<String, Transaction> _userTransactions = {

   'one': Transaction(amount: - 45, date: DateTime.now(), title: 'socks', accountType: 'timr', notes: 'joa' , icon: Icon(Icons.today,), id: 'kololdcd', repeat: 'always'),
   'two': Transaction(amount: - 60, date: DateTime.now(), title: 'present', accountType: 'timr', notes: 'brother' , icon: Icon(Icons.today,), id: 'kolofkfrcd', repeat: 'never'),
  } ;
  


我想创建一个计算每日余额的函数。因此,我需要确定在参数日期中具有特定值的所有Transaction(并可能将它们放入新映射中),然后我需要从参数数量(从我们之前确定的有效Transaction)中访问值并加起来每日余额的所有值。

我是Futter的新手。我试图将 map 转换为列表,并使用函数。在哪里搜索日期数据并访问金额数据,但我感到困惑,因为我需要访问列表中的Widget交易中的值,因此对我来说事情有点复杂。

您对如何开始或如何编写整个方法有什么建议?

最佳答案

您可以将问题的文本直接转换为所需的代码。

int getDailyBalance(DateTime targetDate) {
  // (Start with a list of all the values in the map)
  return _userTransactions.values
  // ...identify all Transaction that have a certain value for the argument date
    .where((t) => t.date == targetDate)
  // Then I need access to the values from the argument amount
    .map((t) => t.amount)
  // ...add up all values for the daily balance
    .reduce((a1, a2) => a1 + a2)
  // (Default to zero if no matching values are found [optional])
    ?? 0;;
}

关于javascript - 如何从 map 中的小部件搜索和访问数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60243873/

相关文章:

android - 使用-sdk :minSdkVersion 16 cannot be smaller than version 23 declared in library [:audioplayers]

javascript - 访问主干获取参数

python - 查找具有最大属性值的对象的键

c# - 从字典返回通用类型(否则返回默认通用值)

android - Flutter markdown 包依赖项没有为我正确安装

flutter - servicestack-dart如何检查 session 是否已经存在?

javascript - React Hooks - 无法获取更新的状态值

javascript - 在多个位置循环显示一个 Sprite 或多个重复图像

javascript - VueJS 使用 NULL 和 'undefined' 值的 Prop 类型验证?

swift - 调用中参数 'dictionary' 缺少参数