英文:
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['student1'].remove('name');
Or if you want to remove all students name use the bleow method
Map data = {
"studet1": {"name": "ajk", "age": "22", "place": "delhi"},
"studet2": {"name": "akmal", "age": "25", "place": "up"}
};
for (int i = 0; i <= data.length - 1; i++) {
data[data.keys.elementAt(i)].remove('name');
}
The output will be
{student1: {age: 22, place: delhi}, student2: {age: 25, place: up}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论