如何从Dart中的地图中删除特定值?

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

How remove the specific value from map in dart?

问题

我是Flutter的初学者,正在学习地图概念。我对地图的方法感到困惑。如何从地图中删除特定的值?

例如:

Map data = {
  "studet1": {"name": "ajk", "age": "22", "place": "delhi"},
  "studet2": {"name": "akmal", "age": "25", "place": "up"}
};

我想从"student1"中删除"name"。

英文:

I am Beginner in flutter, learning map concept. I am confusing map methods. How to delete a specific value from a map?

for example:

Map data = {
  "studet1": {"name": "ajk", "age": "22", "place": "delhi"},
  "studet2": {"name": "akmal", "age": "25", "place": "up"}
};

I want to delete the "name" from "student1".

答案1

得分: 5

data 是一个嵌套的 map,意味着它在 student1 这个键内包含另一个 map

你可以使用 .remove 方法来从 map 中删除一个键:
> 如果存在,从 map 中删除 key 及其关联的值。

void main() {
  Map data ={
    "student1":{
      "name" : "ajk",
      "age":"22",
      "place":"delhi"
    },
    "student2":{
      "name" : "akmal",
      "age":"25",
      "place":"up"
    }
  };
  
  data['student1'].remove('name');
  print(data);
}

打印结果:

{student1: {age: 22, place: delhi}, student2: {name: akmal, age: 25, place: up}}
英文:

data is a nested map, which means that it has another map within the key of student1.

You can use the .remove method to remove a key within a map:
> Removes key and its associated value, if present, from the map.

void main() {
  Map data ={
    "student1":{
      "name" : "ajk",
      "age":"22",
      "place":"delhi"

    },
    "student2":{
      "name" : "akmal",
      "age":"25",
      "place":"up"

    }
  };
  
  data['student1'].remove('name');
  print(data);
}

Prints:

{student1: {age: 22, place: delhi}, student2: {name: akmal, age: 25, place: up}}

答案2

得分: 2

如果您只想删除student1的名字,请使用以下代码:

data['student1'].remove('name');

或者,如果您想删除所有学生的名字,请使用以下方法:

Map data = {
  "student1": {"name": "ajk", "age": "22", "place": "delhi"},
  "student2": {"name": "akmal", "age": "25", "place": "up"}
};
for (int i = 0; i <= data.length - 1; i++) {
  data[data.keys.elementAt(i)].remove('name');
}

输出将是:

{student1: {age: 22, place: delhi}, student2: {age: 25, place: up}}
英文:

If you want to remove only student1 name

Just use data[&#39;student1&#39;].remove(&#39;name&#39;);

Or if you want to remove all students name use the bleow method

   Map data = {
      &quot;studet1&quot;: {&quot;name&quot;: &quot;ajk&quot;, &quot;age&quot;: &quot;22&quot;, &quot;place&quot;: &quot;delhi&quot;},
      &quot;studet2&quot;: {&quot;name&quot;: &quot;akmal&quot;, &quot;age&quot;: &quot;25&quot;, &quot;place&quot;: &quot;up&quot;}
    };
    for (int i = 0; i &lt;= data.length - 1; i++) {
      data[data.keys.elementAt(i)].remove(&#39;name&#39;);
    }

The output will be

{student1: {age: 22, place: delhi}, student2: {age: 25, place: up}}

huangapple
  • 本文由 发表于 2023年1月9日 12:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定