[英]How to remove all same data from a list<String> exept the last ? with flutter
我搜索以從列表中刪除所有相同的數據,除了最后一個
我有一個這樣的輸入列表:
[2020-11-04,note0:blabla, ... data_random ... 2020-11-04,note0:srgg, ... data_random ... ,2020-11-15,note0:test, ... data_random ... , 2020-11-15,note0:test1, ... data_random ... , 2020-11-15,note0:test2, ... data_random ... , 2020-11-15,note0:test3]
我搜索只保留最后一個2020-11-15,note0:test3
並刪除2020-11-04,note0:blabla, 2020-11-04,note0:srgg, 2020-11-15,note0:test, 2020-11-15,note0:test1, 2020-11-15,note0:test2
謝謝你
因為它們在“2020-11-15,note0:”中很常見,所以你可以像這樣使用反向 for 循環並檢查它們是否包含:“2020-11-15,note0:”
List keepTheLast() {
List input = [
"2020-11-15,note0:test",
"... data_random ... ",
"2020-11-15,note0:test1",
"... data_random ...",
"2020-11-15,note0:test2",
"... data_random ... ",
"2020-11-15,note0:test3"
];
bool found = false;
for (int i = input.length - 1; i >= 0; i--)
if (input[i].contains("2020-11-15,note0:"))
found ? input.removeAt(i) : found = true;
print(input.toString());
return input;}
res 將是:
[... data_random ... , ... data_random ..., ... data_random ... , 2020-11-15,note0:test3]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.