Created
July 4, 2019 16:26
-
-
Save nikli2009/12f1402aae5d7445f5a17b3ad55cf399 to your computer and use it in GitHub Desktop.
Dart List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
List<String> usernames = ['lana', 'del', 'rey', 'hope', 'sandoval']; | |
// add => bottom | |
usernames.add('nikk'); | |
print(usernames); //[lana, del, rey, hope, sandoval, nikk] | |
// add all | |
usernames.addAll(['wenyan', 'li']); | |
print(usernames); // [lana, del, rey, hope, sandoval, nikk, wenyan, li] | |
// remove at | |
usernames.removeAt(0); | |
print(usernames); // [del, rey, hope, sandoval, nikk, wenyan, li] | |
// remove with condition | |
usernames.removeWhere((x) => x.contains('an')); | |
print(usernames); // [del, rey, hope, nikk, li] | |
// add => specificed position | |
usernames.insert(3, 'tony'); | |
print(usernames); // [del, rey, hope, tony, nikk, li] | |
// any | |
bool hasDeGuy = usernames.any((x) => x.startsWith(new RegExp(r'(de)'))); | |
print(hasDeGuy); // true | |
// where (filter) | |
var filtered = usernames.where((x) => x.startsWith(new RegExp(r'(de|re|ho|to)'))); | |
print(filtered); // [del, rey, hope, tony] | |
// iteration | |
var handled = filtered.map((original) { | |
return original + '_handled'; | |
}); | |
print(handled); // [del_handled, rey_handled, hope_handled, tony_handled] | |
// every | |
print(handled.every((h) => h.contains('_handled'))); // true | |
// fold (reduce) | |
var folded = handled.fold('user_names:', (prev, item) { | |
return prev + item.replaceAll('handled', ''); | |
}); | |
print(folded); // user_names:del_rey_hope_tony_ | |
// forEach | |
handled.forEach((item) => { | |
print(item) | |
}); | |
// del_handled | |
// rey_handled | |
// hope_handled | |
// tony_handled | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment