Dart 常用的数组操作方法总结

2020-03-2811:07:21编程语言入门到精通Comments3,265 views字数 1951阅读模式

首先,我们准备两组数据,以方便后面使用:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

List<Map> students =[{'name':'tom','age':16},{'name':'jack','age':18},{'name':'lucy','age':20}];

List numbers =[2,8,5,1,7,3];

本文中用到的 students 及 numbers 均指的是这里已经定义好的,后面不再重复。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

forEach()
可以遍历一个数组或者是一个map文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

students.forEach((student)=>print(student));

Map tom ={'name':'tom','age':16};
tom.forEach((key, value)=>print(key +' - '+ value.toString()));

Map()
可以用来操作已知数组里的每一项,然后返回一个新数组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

var messages = students.map((student)=>'Hello '+ student['name']).toList();print(messages);

contains()
用于判断数组是否包含某个元素文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

print(students.contains(5));

reduce(), fold()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

  • reduce() 将数组中的每一个值与前面返回的值相加,最后返回相加的总和
  • fold() 用法跟 reduce() 基本一样,只不过是可以提供一个初始值
var sum = numbers.reduce((curr, next)=> curr + next);print(sum);

var sum2 = numbers.fold(10,(curr, next)=> curr + next);print(sum2);

every()
用于判断数组中的每一项是否均达到了某个条件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

var isAgeOver20 = students.every((student)=> student["age"]>20);print(isAgeOver20);

var isAgeOver15 = students.every((student)=> student["age"]>15);print(isAgeOver15);

where(), firstWhere(), singleWhere()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

  • where() 返回数组中满足给定条件的元素集合
  • firstWhere() 返回数组中满足给定条件的第一个元素
  • singleWhere() 返回数组中满足给定条件的唯一一个元素,若有多个元素满足条件会抛出异常
var ageOver16 = students.where((student)=> student["age"]>16);print(ageOver16.toList());

var ageFirstOver16 = students.firstWhere((student)=> student["age"]>16, orElse:()=> null);print(ageFirstOver16);

var ageUnder20 = students.singleWhere((student)=> student["age"]<16, orElse:()=> null);print(ageUnder20);

take(), skip()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

  • take(n) 从数组里取 n 个元素
  • skip(n) 跳过数组中的 n 个元素
List arr =[1,3,5,2,7,9];print(arr.take(3).toList());print(arr.skip(4).toList());print(arr.take(3).skip(2).take(1).toList());

()
克隆一个数组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

List arr =[1,3,5,2,7,9];

var clonedArr = List.from(arr);print(clonedArr);

expand()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

var arr1 =[[2,5],[7],[11,12]];
var flattened = arr1.expand((item)=> item).toList();print(flattened);

var arr2 =[2,5,8];
var computed = arr2.expand((item)=>[item *8]).toList();print(computed);
var computed2 = arr2.map((item)=> item *8).toList();print(computed2);

add(), addAll()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

  • add() 向数组中添加一个元素
  • addAll() 向数组中添加另一个数组的所有元素
>var arr1 =[1,3,5,9,2,1];

arr1.add(10);print(arr1);
arr1.addAll([15,21]);print(arr1);

作者:三也视界文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/17951.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/17951.html

Comment

匿名网友 填写信息

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

确定