1、list的遍历
var list = [1, 2, 3, 4, 5];
for (var i = 0; i <= list.length; i++) {
print(i);
}
for (var item in list) {
print(item);
}
list.forEach((element) {
print(element);
});
//where判断大于3,返回值为(4,5),toList()组成新的list
var newList = list.where((element) => element > 3).toList();
print(newList);
//any 判断list的数据是否有大于3的值,返回值为bool,true
var checkAny = list.any((element) => element > 3);
print(checkAny);
//every 判断list里面的数据是否都大于3,返回值为bool, false
var checkEvery = list.every((element) => element > 3);
print(checkEvery);
2、map的遍历
var map = {
"name":"张三",
"age":22,
"work":"上班族",
"sex":"男",
};
for(var item in map.keys){
print(item);
}
for(var item in map.values){
print(item);
}
map.forEach((key, value) {
print("$key---$value");
});
var newList = map.values.where((element) => element==22);
print(newList.toList());
//判断map的值是否有张三,返回值bool
var checkAny = map.values.any((element) => element=="张三");
print(checkAny);
//判断map的值是否全部为张三,返回值bool
var checkEvery = map.values.every((element) => element=="张三");
print(checkEvery);
网友评论