Comparing Lists with different types for duplicates returns only one item.

huangapple go评论55阅读模式
英文:

Comparing Lists with different types for duplicates returns only one item

问题

以下是您提供的代码的翻译部分:

我有两个包含数据的列表。
第一个列表是自定义类型 TodayTask

    todaysTodayTasks {
      task1 {title: "清理房间", details: "除尘、扫地、拖地" ...}
      task1 {title: "购物", details: "面粉、胡萝卜" ...}
    }

第二个列表是 RepeatingTask 类型,但其中包含 TodayTask 属性。

    repeatingTasks {
      task1 {repeatfreq:22, repeat:true, task: {title: "清理房间", details: "除尘、扫地、拖地"} ... },
      task1 {repeatfreq:52, repeat:true, task: {title: "购物", details: "面粉、胡萝卜"} ... },
    }

现在我想检查 todaysTodayTasks 列表是否包含在 `repeatingTasks.task` 中的任何条目。如果它不包含任何重复的任务,请将那些缺失的重复任务添加到 todaysTodayTasks 中,否则不执行任何操作。例如,首先 todaysTodayTasks 只包含以下条目:

    todaysTodayTasks {
      task1 {title: "清理房间", details: "除尘、扫地、拖地" ...}
    }

因此,代码将向 todaysTodayTasks 列表添加以下 repeatingTasks 列表的条目:

    task1 {title: "购物", details: "面粉、胡萝卜" ...}

这是我目前拥有的代码。当没有条目开始时,它可以正常工作,但是当 todaysTodayTasks 中已经有条目时,它会继续从 repeatingtasks 添加条目,但从第二个条目开始。它检测到第一个条目已经存在并且不会添加它,但会添加重复任务列表中的其余条目。

``` dart
if (todaysTodayTasks.isEmpty) {
   print("今天的任务为空。");
   for (RepeatingTask task in repeatingTasks) {
     if (task.TodayTask != null) {
       print("添加今天的重复任务: ${task.TodayTask!.title}");
       // 在此处添加 task.TodayTask 到 todocolleciton
       TodayTask newTodayTask = TodayTask(
           title: task.TodayTask!.title,
           details: task.TodayTask!.details,
           priority: task.TodayTask!.priority,
           isComplete: false,
           date: date,
           tags: task.TodayTask!.tags,
           isRepeating: true);
       writeTodoData(newTodayTask);
     }
   }
}else{
 for (RepeatingTask task in repeatingTasks) {
   for (TodayTask item in todaysTodayTasks) {
     print("当前 TodayTask: ${item.title}");
     print("当前任务: ${task.TodayTask!.title}");
     if (task.TodayTask != null) {
       if (item.title != task.TodayTask!.title &&
           item.details != task.TodayTask!.details) {
         print("添加今天的重复任务: ${task.TodayTask!.title}");
         // 在此处添加 task.TodayTask 到 todocolleciton
         TodayTask newTodayTask = TodayTask(
             title: item.title,
             details: item.details,
             priority: item.priority,
             isComplete: false,
             date: date,
             tags: item.tags,
             isRepeating: true);
         writeTodoData(newTodayTask);
       }
     }
   }
 }
}

Comparing Lists with different types for duplicates returns only one item.


希望这有助于您理解代码的翻译部分。如果您有任何其他问题,请随时提出。
<details>
<summary>英文:</summary>
I have two Lists with data.
First list is of custom type TodayTask
todaysTodayTasks {
task1 {title: &quot;clean room&quot;, details: &quot;dust, sweep, mop&quot; ...}
task1 {title: &quot;shopping&quot;, details: &quot;flour, carrots&quot; ...}
}
Second list is of RepeatingTask Type but has a TodayTask property inside it.
repeatingTasks {
task1 {repeatfreq:22, repeat:true, task: {title: &quot;clean room&quot;, details: &quot;dust, sweep, mop&quot;} ... },
task1 {repeatfreq:52, repeat:true, task: {title: &quot;shopping&quot;, details: &quot;flour, carrots&quot;} ... },
}
Now what I want is to check if todaysTodayTasks list contains any of the entries in `repeatingTasks.task`. if it does not contain any of the repeating tasks, add those missing repeating tasks to todaysTodayTasks otherwise do nothing. For example to start with todaysTodayTasks only has the following entry:
todaysTodayTasks {
task1 {title: &quot;clean room&quot;, details: &quot;dust, sweep, mop&quot; ...}
}
So the code will add the following repeatingTasks list to todaysTodayTasks list which is 
task1 {title: &quot;shopping&quot;, details: &quot;flour, carrots&quot; ...}
Here the is code I currently have. Now it works when there is no entry to begin with, but when there are entries already inside todaysTodayTasks, it keeps adding entries from the repeatingtasks but starting from the 2nd entry. it detects that the first entry is there and doesnt add that. but adds the rest of the entries from the repeating task list.
``` lang-dart
if (todaysTodayTasks.isEmpty) {
print(&quot;today tasks empty.&quot;);
for (RepeatingTask task in repeatingTasks) {
if (task.TodayTask != null) {
print(&quot;ADD REPEATING TASK FOR TODAY: ${task.TodayTask!.title}&quot;);
//add task.TodayTask to todocolleciton here
TodayTask newTodayTask = TodayTask(
title: task.TodayTask!.title,
details: task.TodayTask!.details,
priority: task.TodayTask!.priority,
isComplete: false,
date: date,
tags: task.TodayTask!.tags,
isRepeating: true);
writeTodoData(newTodayTask);
}
}
}else{
for (RepeatingTask task in repeatingTasks) {
for (TodayTask item in todaysTodayTasks) {
print(&quot;Current TodayTask: ${item.title}&quot;);
print(&quot;Current Task: ${task.TodayTask!.title}&quot;);
if (task.TodayTask != null) {
if (item.title != task.TodayTask!.title &amp;&amp;
item.details != task.TodayTask!.details) {
print(&quot;ADD REPEATING TASK FOR TODAY: ${task.TodayTask!.title}&quot;);
//add task.TodayTask to todocolleciton here
TodayTask newTodayTask = TodayTask(
title: item.title,
details: item.details,
priority: item.priority,
isComplete: false,
date: date,
tags: item.tags,
isRepeating: true);
writeTodoData(newTodayTask);
}
}
}
}
}

Comparing Lists with different types for duplicates returns only one item.

答案1

得分: 1

Create a set of titles of TodayTasks in todaysTodayTasks list

Set todayTaskTitles = Set.from(todaysTodayTasks.map((task) => task.title));

for (RepeatingTask task in repeatingTasks) {
if (task.TodayTask != null) {
if (!todayTaskTitles.contains(task.TodayTask!.title)) {
print("ADD REPEATING TASK FOR TODAY: ${task.TodayTask!.title}");
// add task.TodayTask to todocolleciton here
TodayTask newTodayTask = TodayTask(
title: task.TodayTask!.title,
details: task.TodayTask!.details,
priority: task.TodayTask!.priority,
isComplete: false,
date: date,
tags: task.TodayTask!.tags,
isRepeating: true);
writeTodoData(newTodayTask);
}
}
}

英文:

Create a set of titles of TodayTasks in todaysTodayTasks list

Set&lt;String&gt; todayTaskTitles = Set&lt;String&gt;.from(todaysTodayTasks.map((task) =&gt; task.title));
for (RepeatingTask task in repeatingTasks) {
if (task.TodayTask != null) {
if (!todayTaskTitles.contains(task.TodayTask!.title)) {
print(&quot;ADD REPEATING TASK FOR TODAY: ${task.TodayTask!.title}&quot;);
// add task.TodayTask to todocolleciton here
TodayTask newTodayTask = TodayTask(
title: task.TodayTask!.title,
details: task.TodayTask!.details,
priority: task.TodayTask!.priority,
isComplete: false,
date: date,
tags: task.TodayTask!.tags,
isRepeating: true);
writeTodoData(newTodayTask);
}
}
}

huangapple
  • 本文由 发表于 2023年5月13日 15:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241471.html
匿名

发表评论

匿名网友

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

确定