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

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

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

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


forEach()

students.forEach((student) => print(student));
// {name: tom, age: 16}
// {name: jack, age: 18}
// {name: lucy, age: 20}

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


Map()

var messages = students.map((student) => 'Hello ' + student['name']).toList();
print(messages);
// [Hello tom, Hello jack, Hello lucy]


contains()

print(students.contains(5));
// true


reduce(), fold()

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

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


every()

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

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


where(), firstWhere(), singleWhere()

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

var ageFirstOver16 = students.firstWhere((student) => student["age"] > 16, orElse: () => null);
print(ageFirstOver16);
// {name: jack, age: 18}

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


take(), skip()

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

print(arr.take(3).toList());
// [1, 3, 5]
print(arr.skip(4).toList());
// [7, 9]
print(arr.take(3).skip(2).take(1).toList());
// [5]


List.from()

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

var clonedArr = List.from(arr);
print(clonedArr);
// [1, 3, 5, 2, 7, 9]


expand()

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

var arr2 = [2, 5, 8];
var computed = arr2.expand((item) => [item * 8]).toList();
print(computed);
// [16, 40, 64]

// 当对每一项进行计算时类似于 map()
var computed2 = arr2.map((item) => item * 8).toList();
print(computed2);
// [16, 40, 64]


>var arr1 = [1, 3, 5, 9, 2, 1];

print(arr1);
// [1, 3, 5, 9, 2, 1, 10]
print(arr1);
// [1, 3, 5, 9, 2, 1, 10, 15, 21]


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客