【问题标题】:flutter: match list value to map list key value颤振:将列表值匹配到映射列表键值
【发布时间】:2021-09-02 05:47:24
【问题描述】:

这里是新手。有谁知道我如何将列表 x 的值与列表 y 的键 [“id”] 的值相匹配?

提前谢谢你!感谢任何帮助。 =)

list x = ["open_box", "close_box"];
list y = 
[
    {
        "id": "open_box",
        "name": "Open",
        "action": ["open_box"]
    },
    {
        "id": "close_box",
        "name": "Close",
        "action": ["open_box", "close_box", "pull_box"]
    },
    {
        "id": "pull_box",
        "name": "Pull",
        "action": ["open_box", "close_box", "pull_box", "push_box"]
    }
]

【问题讨论】:

    标签: list flutter dictionary key


    【解决方案1】:
        list x = ["open_box", "close_box"];
        list y = 
        [
            {
                "id": "open_box",
                "name": "Open",
                "action": ["open_box"]
            },
            {
                "id": "close_box",
                "name": "Close",
                "action": ["open_box", "close_box", "pull_box"]
            },
            {
                "id": "pull_box",
                "name": "Pull",
                "action": ["open_box", "close_box", "pull_box", "push_box"]
            }
        ];
        var i,j;
        var yy=jsonDecode(y);
    for(j=0;j<=x.length;j++){
        for(i=0;i<=yy.length;i++){
    
        if(yy[i]['id'].contains(x[j])){
        
        //Do something if true
        print(yy[i]);
        
        }
        }
    }
    

    此代码比较 x[0] 表示“open_box”与所有 y 的 Maped 数据列表,它将在条件为真时打印索引值

    【讨论】:

      【解决方案2】:

      使用for in循环x,最后可以使用List中的where方法

      for (String action in x) {
        dynamic temp = y.where((data) => data['id'] == action);
        if (temp.isNotEmpty) {
          // do someting
          print(temp);
        }
      }
      
      

      参考:Dart Documentation - List

      【讨论】:

        【解决方案3】:

        List 提供了一个where 方法,允许您根据条件过滤列表。这里我使用y 列表的where 方法并在x 列表中搜索y 列表中当前项的id 值。我检查了x 列表的搜索长度,如果它大于零,那么我们在x 列表中找到了该id 的匹配项。 z 将在y 列表中保存与x 列表中的ID 匹配的项目列表。

        List x = ["open_box", "close_box"];
        List y = 
        [
                {
                        "id": "open_box",
                        "name": "Open",
                        "action": ["open_box"]
                },
                {
                        "id": "close_box",
                        "name": "Close",
                        "action": ["open_box", "close_box", "pull_box"]
                },
                {
                        "id": "pull_box",
                        "name": "Pull",
                        "action": ["open_box", "close_box", "pull_box", "push_box"]
                }
        ];
        var z = y.where((data) => x.where((value) => value==data["id"]).length>0);
        print(z);
        

        这是预期的输出

        I/flutter ( 6086): ({id: open_box, name: Open, action: [open_box]}, {id: close_box, name: Close, action: [open_box, close_box, pull_box]})
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-13
          • 1970-01-01
          • 1970-01-01
          • 2018-06-09
          • 2018-09-21
          相关资源
          最近更新 更多