简体   繁体   English

如何提取地图列表中的数据并将其转换为 dart 中的地图

[英]how to extract data inside the list of maps and convert it into maps in dart

How can extract data inside the list of maps and convert it into maps in the dart.如何提取地图列表中的数据并将其转换为飞镖中的地图。 Like I have a List of maps ================================================================================================================================================================================================就像我有一个地图列表========================================== ================================================== ================================================== ==================================================

[
  {"business_id":"2",
   "business_title":"Spotify",
   "business_phone":"(055) 3733783",
   "business_email":"Usamashafiq309@gmail.com",
   "business_website":"www.spotify.com",
   "business_address":"Spotify AB, Regeringsgatans bro, Stockholm, Sweden",
   "business_address_longitude":"18.0680873",
   "business_address_latitude":"59.33096949999999",
   "business_image":"5f84c7a4bbbd0121020201602537380.png",
   "business_created_at":"2020-10-20 15:40:17",
   "business_category_id":"2",
   "cat_id":"2",
   "cat_title":"Gym",
   "cat_image":"280920201601308237.png"}
    ,{"business_id":"2",
   "business_title":"Spotify",
   "business_phone":"(055) 3733783",
   "business_email":"Usamashafiq309@gmail.com",
   "business_website":"www.spotify.com",
   "business_address":"Spotify AB, Regeringsgatans bro, Stockholm, Sweden",
   "business_address_longitude":"18.0680873",
   "business_address_latitude":"59.33096949999999",
   "business_image":"5f84c7a4bbbd0121020201602537380.png",
   "business_created_at":"2020-10-20 15:40:17",
   "business_category_id":"2",
   "cat_id":"2",
   "cat_title":"Gym",
   "cat_image":"280920201601308237.png"}
 ]

and convert it like this并像这样转换它

[ {"business_id":"2",
   "business_title":"Spotify",},
 {"business_id": "1",
            "business_title": "Pizza Hut",},

]

You can use the map function to apply a function to each element of a list.您可以使用map函数将函数应用于列表的每个元素。 Then you can create a submap with your map.然后您可以使用您的地图创建子地图。

Here is a quick exemple:这是一个快速示例:

void main() async {
  List l = [
    {
      "business_id": "2",
      "business_title": "Spotify",
      "business_phone": "(055) 3733783",
    },
    {
      "business_id": "1",
      "business_title": "Pizza Hut",
      "business_phone": "(055) 9999999",
    }
  ];

  print(extractMap(l));
}

List extractMap(List l) {
  return l
      .map((element) => Map.fromEntries([
            MapEntry('business_id', element['business_id']),
            MapEntry('business_title', element['business_title']),
          ]))
      .toList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM