博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Dart] Manipulate Lists/Arrays in Dart
阅读量:4983 次
发布时间:2019-06-12

本文共 1621 字,大约阅读时间需要 5 分钟。

We will learn how to work with Lists using a variety of methods made available in the dart:core library. We will explore the top methods for working with List type collections.

Learn more about Lists at 

 

Array to list:

because .map return iterable.

var fruits = ['banana', 'pineapple', 'orange', 'watermelon', 'apple'];  var fiboNumbers = [1, 2, 3, 5, 8, 13, 21];  List
> users = [ { 'name': 'John', 'age': 18 }, { 'name': 'Jane', 'age': 21 }, { 'name': 'Mary', 'age': 23 }, ]; // array to list var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList(); print(mappedFruits);

 

reduce vs fold:

reduce: doesn't provide the init value, but 'fold' does:

const initialValue = 10;  var sum2 = fiboNumbers.fold(initialValue, (curr, next) => curr + next);  print( sum2 );    var sum = fiboNumbers.reduce((curr, next) => curr + next);  print( sum );

 

filtering:

var over21s = users.where((user) => user['age'] > 21);  print( over21s.length );    var nameJ = users.firstWhere((user) => user['name'].startsWith('J'), orElse: () => null);  print( nameJ );    var under18 = users.singleWhere((user) => user['age'] < 18, orElse: () => {
'error': 'Not Found'}); print( under18 );

 

take & skip:

print( fiboNumbers.take(3).toList() );  print( fiboNumbers.skip(5).toList() );  print( fiboNumbers.take(3).skip(2).take(1).toList() );

 

expend: the same as flatMap in JS

var flattened = [[1, 2], [3, 4]].expand((pair) => pair).toList();  print( flattened );    var duplicated = fiboNumbers.expand((i) => [i, i]).toList();  print( duplicated );

 

转载于:https://www.cnblogs.com/Answer1215/p/11459398.html

你可能感兴趣的文章
css样式重置方案 -解决浏览器差异
查看>>
distpicker使用记录
查看>>
[BZOJ3282]Tree(LCT)
查看>>
最终版详细设计
查看>>
GenePix Pro 3.0
查看>>
html移动端 -- meta-模板 + rem
查看>>
js-控制浏览器和移动端的后退按钮 . popstate
查看>>
[QT][SQLITE]学习记录二 日期查询
查看>>
hdu 4455 Substrings
查看>>
web传参
查看>>
Python 查找binlog文件
查看>>
Git——如何将本地项目提交至远程仓库
查看>>
Convert CString to std::string
查看>>
3 - Selenium元素定位和操作
查看>>
GCC C语言 DLL范例,含源码
查看>>
冲刺第一天(补发)
查看>>
iOS开发Xcode中切换显示语言实现国际化
查看>>
C++模板学习
查看>>
nginx
查看>>
大数据平台搭建-hadoop集群的搭建
查看>>